MySQL查询这一篇就够了 (2)

例4:查询男生的总年龄

select sum(age) from students where gender=1; -- 平均年龄 select sum(age)/count(*) from students where gender=1; 平均值

avg(列)表示求此列的平均值

例5:查询未删除女生的编号平均值

select avg(id) from students where is_delete=0 and gender=2;

4. 分组 group by

group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组

group by可用于单个字段分组,也可用于多个字段分组

group by + group_concat()

group_concat(字段名)可以作为一个输出字段来使用,

表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合

select gender from students group by gender; +--------+ | gender | +--------+ | 男 | | 女 | | 中性 | | 保密 | +--------+ select gender,group_concat(name) from students group by gender; +--------+-----------------------------------------------------------+ | gender | group_concat(name) | +--------+-----------------------------------------------------------+ | 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 | | 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 | | 中性 | 金星 | | 保密 | 凤姐 | +--------+-----------------------------------------------------------+ select gender,group_concat(id) from students group by gender; +--------+------------------+ | gender | group_concat(id) | +--------+------------------+ | 男 | 3,4,8,9,14 | | 女 | 1,2,5,7,10,12,13 | | 中性 | 11 | | 保密 | 6 | +--------+------------------+ group by + 集合函数

通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作

select gender,group_concat(age) from students group by gender; +--------+----------------------+ | gender | group_concat(age) | +--------+----------------------+ | 男 | 29,59,36,27,12 | | 女 | 18,18,38,18,25,12,34 | | 中性 | 33 | | 保密 | 28 | +--------+----------------------+ 分别统计性别为男/女的人年龄平均值 select gender,avg(age) from students group by gender; +--------+----------+ | gender | avg(age) | +--------+----------+ | 男 | 32.6000 | | 女 | 23.2857 | | 中性 | 33.0000 | | 保密 | 28.0000 | +--------+----------+ 分别统计性别为男/女的人的个数 select gender,count(*) from students group by gender; +--------+----------+ | gender | count(*) | +--------+----------+ | 男 | 5 | | 女 | 7 | | 中性 | 1 | | 保密 | 1 | +--------+----------+ group by + having

having 条件表达式:用来分组查询后指定一些条件来输出查询结果

having作用和where一样,但having只能用于group by

select gender,count(*) from students group by gender having count(*)>2; +--------+----------+ | gender | count(*) | +--------+----------+ | 男 | 5 | | 女 | 7 | +--------+----------+ group by + with rollup

with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和

select gender,count(*) from students group by gender with rollup; +--------+----------+ | gender | count(*) | +--------+----------+ | 男 | 5 | | 女 | 7 | | 中性 | 1 | | 保密 | 1 | | NULL | 14 | +--------+----------+ select gender,group_concat(age) from students group by gender with rollup; +--------+-------------------------------------------+ | gender | group_concat(age) | +--------+-------------------------------------------+ | 男 | 29,59,36,27,12 | | 女 | 18,18,38,18,25,12,34 | | 中性 | 33 | | 保密 | 28 | | NULL | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 | +--------+-------------------------------------------+ 5. 获取部分行

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzfpd.html