group by a:将所有具有相同a字段值的记录放到一个分组里;
group by a,b :将所有具有相同a字段值和b字段值的记录放到一个分组里
count( case when sex=‘1‘ then ‘男‘) as count1,
count( case when sex=‘2‘ then ‘女‘) as count2,
使用语法:
SELECT
CASE WHEN salary <= 500 THEN ‘1‘
WHEN salary > 500 AND salary <= 600 THEN ‘2‘
WHEN salary > 600 AND salary <= 800 THEN ‘3‘
WHEN salary > 800 AND salary <= 1000 THEN ‘4‘
ELSE NULL END salary_class, -- 别名命名
COUNT(*) FROM Table_A
GROUP BY
CASE WHEN salary <= 500 THEN ‘1‘
WHEN salary > 500 AND salary <= 600 THEN ‘2‘
WHEN salary > 600 AND salary <= 800 THEN ‘3‘
WHEN salary > 800 AND salary <= 1000 THEN ‘4‘
ELSE NULL END;
mysql-----group by 对多个字段进行分组
原文:https://www.cnblogs.com/cgy-home/p/11537659.html