首页 > 技术文章 > 基础语法

Amy1205 2017-03-13 22:17 原文

基本语法

  Select 字段1、字段2、字段3……from表where条件

查询语句

  关键字:select

  用法:

    查询所有:Select * from 表名  例:Select * from stuInfo

    查询部分字段:Select stuName,stuSex from stuInfo

去重复行

  关键字:distinct

  用法:select distinct 列名1 from 表名  例:Select distinct depart from  teacher

  注意:distinct必须写在所有列前面

区间语句

  关键字:between

  用法:Select * from score where degree between 60 and 80

指定条件语句:

  关键字:in

  用法:Select * from score where degree in (80,85,90)

and语句:

  关键字:and

  用法:Select * from score where degree>=60 and degree<=80

or语句:

  关键字:or

  用法:Select * from score where degree>=80 or sex='女'

排序语句:

  关键字:order by

  用法:Select * from student order by class desc

  注:asc(升序,默认),desc(降序)

汇总语句:

  关键字:count(*)

  题目:查询95031班的学生人数

  解题:select count(*) ad CNT from student where class='95031'

求一列的最大(小)值:

  关键字:max/min(字段名)

  题目:查取score表中的最高(低)分

  解题:select max/min(degree) from score

求平均值:

  关键字:avg(字段名)

  题目:查询‘3-105’号课程的平均分

  解题:select avg(degree) as degreeAvg from score where cno='3-105'

分组语句(having 是条件):

  关键字:group by(字段名) having 条件

  题目:查询score表中至少有5名学生选修课是以‘3’开头的,平均分数

  解题:select avg(degree) ad degreeAvg from score where cno like '3%' group by cno having count(*) 5

推荐阅读