码迷,mamicode.com
首页 > 其他好文 > 详细

单表查询基础内容

时间:2020-01-02 22:11:49      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:child   having   18C   筛选   wrap   avg   条件   单表查询   div   

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;
聚合函数 
max() 求最大值
min() 求最小值
avg() 求平均值
sum() 求和
count() 求总个数
having 分组之后筛选条件的 一般跟聚合函数连用 
order by 排序
desc 降序
asc 升序
select * from employee order by salary desc;
limit 取限定条数
select * from employee order by salary desc limit 2; 开头取两条
select * from employee order by salary desc limit 1,2; 从第二条开始取两条
简单练习题 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

单表查询基础内容

标签:child   having   18C   筛选   wrap   avg   条件   单表查询   div   

原文地址:https://www.cnblogs.com/Darry-Ring/p/12141990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!