码迷,mamicode.com
首页 > 数据库 > 详细

mysql中一张(居民)表按年龄段查询数据

时间:2018-03-19 00:35:33      阅读:678      评论:0      收藏:0      [点我收藏+]

标签:back   bsp   else   sid   ima   gpo   group by   mysql语句   style   

知识点:

    用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中一张(居民)表按年龄段查询数据

标签:back   bsp   else   sid   ima   gpo   group by   mysql语句   style   

原文地址:https://www.cnblogs.com/shuaifing/p/8598254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!