mysql-group by分组用法
sql执行顺序
select 查询列表 -- 7
from 表 -- 1
join 表2 -- 2
on 连接条件 -- 3
where 筛选条件 -- 4
group by 分组字段 -- 5
having 分组后的筛选 -- 6
order by 排序的字段 -- 8
limit offset, size; -- 9
SELECT * FROM user_t
-
group by + group_concat()
select sex, group_concat(username) from user_t group by sex;
-
group by可用于多个字段分组:多个字段完全一致为一组
select sex, address, group_concat(username), group_concat(address) from user_t group by sex, address;
-
group by + 聚合函数
select sex,group_concat(age), SUM(age), COUNT(*), avg(age) from user_t group by sex;
-
group by + having:用来过滤分组结果
select age, group_concat(username),count(*) from user_t group by age HAVING count(*)=3;