sql查询 —— 分组
1 -- 分组
2 -- group by
3 -- 分组只有与聚合函数一起使用才能发挥作用
4 -- 分组只限于字段分明 例如 性别 ,部门,
5 --列出所有性别
6 select gender from student group by gender;
7 --列出所有性别 及数量
8 select gender,count(*) from student group by gender;
9
10 --group_concat
11 -- 统计的数量
12 --列出所有性别人名
13 select gender,group_concat(name) from student group by gender;
14 --列出所有男性人名
15 select gender,group_concat(name) from student where gender='man' group by gender;
16 --列出所有性别分别的数量和分别的姓名
17 select gender as '性别',count(*) as '数量',group_concat(name) from student group by gender;
18 --列出所有年龄大于10,性别,数量及分别的姓名
19 select gender as '性别',count(*) as '数量',group_concat(name) from student where age>10 group by gender;
20
21
22 -- having
23 -- having 是在查询结果中,再查询
24 -- 查询平均年龄超过20的性别 及姓名
25 select gender,group_concat(name),avg(age) from student group by gender having avg(age)>20;
26 -- 查询每种性别人数大于2的,
27 select gender,group_concat(name) from student group by gender having count(*)>2;