mysql统计年龄段分布

阅读更多

mysql 根据生日统计年龄分布。birthday字段为时间戳

select '(-∞,20)' value,sum(case when user_age<20 then 1 else 0 end) counts from (
        SELECT FROM_UNIXTIME(birthday, '%Y-%m-%d') as t_birth,CURDATE(), YEAR(CURDATE())-YEAR(FROM_UNIXTIME(birthday, '%Y-%m-%d') ) as user_age from wp_fuser where 1
    ) TA
union
select '[20,30)' value,sum(case when (user_age>=20 and user_age<30) then 1 else 0 end) counts  from  (
        SELECT FROM_UNIXTIME(birthday, '%Y-%m-%d') as t_birth,CURDATE(), YEAR(CURDATE())-YEAR(FROM_UNIXTIME(birthday, '%Y-%m-%d') ) as user_age from wp_fuser where 1
    ) TA
union
select '[30,35)' value,sum(case when (user_age>=30 and user_age<35) then 1 else 0 end) counts from (
        SELECT FROM_UNIXTIME(birthday, '%Y-%m-%d') as t_birth,CURDATE(), YEAR(CURDATE())-YEAR(FROM_UNIXTIME(birthday, '%Y-%m-%d') ) as user_age from wp_fuser where 1
    ) TA
union
select '[35,+∞)' value,sum(case when user_age>=35 then 1 else 0 end) counts from (
        SELECT FROM_UNIXTIME(birthday, '%Y-%m-%d') as t_birth,CURDATE(), YEAR(CURDATE())-YEAR(FROM_UNIXTIME(birthday, '%Y-%m-%d') ) as user_age from wp_fuser where 1
    ) TA

 

最终结果:

mysql统计年龄段分布_第1张图片

  • mysql统计年龄段分布_第2张图片
  • 大小: 2.7 KB
  • 查看图片附件

你可能感兴趣的:(mysql,统计,年龄段)