DQL分组查询(group by)
语法
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
where和having的区别
-
执行时机不同: where是在分组之前进行过滤,不满足where条件,不参与分组; having是分组之后对结果进行过滤
-
判断条件不同:where不能对聚合函数进行判断,having可以
根据员工性别分组, 统计男女员工数量
select gender, count(*) from workers group by gender;
根据性别分组,并统计平均年龄
select gender, avg(age) from workers group by gender;
查询年龄>45的员工, 并按入职日期分组, 获取员工数量>=2的入职日期
select entryDate, count(*) dateCount
from workers
where age>45
group by entryDate
#分组后过滤
having dateCount>=2;
标签:group,gender,分组,MYSQL,DQL,where,having,select
From: https://www.cnblogs.com/HIK4RU44/p/18062024