1. 程式人生 > >mysql中一張(居民)表按年齡段查詢數據

mysql中一張(居民)表按年齡段查詢數據

log clas rom count 年齡段 group by 中一 span OS

知識點:

用mysql,按年齡段查詢一張居民的數據(各年齡段居民的個數)

1.如:查詢resident(居民表),按照各年齡段,統計人數技術分享圖片

2.mysql語句如下:

select ageproportion as ‘年齡段‘,count(*) as ‘人數‘ from
(
SELECT
CASE
when age>0 and age<=10 then ‘0-10歲‘
when age>10 and age<=20 then ‘10-20歲‘
when age>20 and age<=30 then ‘20-30歲‘
when age>30 and age<=40 then ‘30-40歲‘
when age>40 and age<=50 then ‘40-50歲‘
when age>50 and age<=60 then ‘50-60歲‘
else ‘60歲以上‘

END
as ageproportion from resident
)a GROUP BY ageproportion

3.查詢結果:

技術分享圖片

mysql中一張(居民)表按年齡段查詢數據