单表查询基础内容

where子句中可以使用
1. 比较运算符:>、<、>=、<=、!=
2. between 80 and 100 :值在80到100之间 :包括两边的
3. in(80,90,100)值是80或90或100
4. like 'xiao%'可以是%或者_, %代表任意多字符,_表示一个字符 
x%, x开头
%a% , 包括a
%t t结尾
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
分组 group by
小窍门,每的后面字段是分组的字段 
分组之后select 后面不能出现任何除了分组依据字段外的其他字段
但是可以和聚合函数连用
 select group_concat(name),post,count(id) from employee group by post;
简单练习题 39.100.47.247
class 1 2 3 
每个班级的人数
select cls, count(name) group by cls;
年龄大于20 的同学的信息显示2条
select * from class where age>20 limit 2;
年龄大于20 ,分数从高到低排序的同学的信息显示2条
select * from class where age>20 order by score desc limit 2
 每个班级的平均成绩
select cls,avg(score) from class group by cls;
年龄小于18岁,且成绩大于70分的同学信息
select * from class where age < 18 and score > 70;
年龄小于20岁,且平均成绩大于70分的同学信息
select name,avg(score) from class where age < 20 group by name having avg(score) > 70;
选出成绩在70到90之间的同学信息并按年龄降序排列
select * from class where score between 70 and 90 order by age desc;
筛选出成绩不小于80分的信息。从第2条显示,显示3条。
select * from class where score>=80 limit 1,3; 
每个班级年龄大于20 的同学数量,及最高成绩。
select cls, max(score), count(name) from class where age>20 group by cls;
重点中的重点:关键字的执行优先级
1 from
2 where
条件里面不能出现聚合函数
分组之前筛选的条件
3 group by
4 聚合函数(max(id)等)
5 having 一般和聚合函数连用
分组之后筛选条件
6 select 结果
7 distinct 去重
8 order by
9 limit
posted @ 2020-01-02 20:54  情~睿  阅读(207)  评论(0编辑  收藏  举报