统计男女生人数--sql
统计男女生人数--sql
传统方法
-- 男性人口
SELECT pref_name AS 地区,
SUM(population) AS 男
FROM `user`
WHERE u_sex = '1'
GROUP BY pref_name;
-- 女性人口
SELECT pref_name AS 地区,
SUM(population) AS 女
FROM `user`
WHERE u_sex ='2'
GROUP BY pref_name;
CASE表达式
-- 统计 人口数据
SELECT pref_name AS 地区,
-- 男性人口
SUM(CASE WHEN u_sex='1' THEN population ELSE 0 END ) AS 男,
-- 女性人口
SUM(CASE WHEN u_sex='2' THEN population ELSE 0 END ) AS 女
FROM `user`
GROUP BY pref_name