MySQL根据年龄分组查人数

根据年龄分组查人数

第一种

SELECT (CASE
WHEN a.age < 0 THEN '负数'
WHEN a.age IS NULL THEN '未知'
WHEN a.age BETWEEN 0 AND 17 THEN '18岁以下'
WHEN a.age BETWEEN 18 AND 25 THEN '18-25岁'
WHEN a.age BETWEEN 26 AND 35 THEN '26-35岁'
WHEN a.age BETWEEN 36 AND 45 THEN '36-45岁'
WHEN a.age BETWEEN 46 AND 60 THEN '45-60岁'
WHEN a.age >= 61 THEN '60岁以上'
END) AS s,
COUNT(*) 'value' FROM
(SELECT TIMESTAMPDIFF(YEAR,tt_person.birthday,CURDATE()) age FROM tt_person) a
GROUP BY s;

第二种

SELECT (CASE
WHEN a.age IS NULL THEN '未知'
WHEN a.age BETWEEN 18 AND 25 THEN '18-25岁'
WHEN a.age BETWEEN 26 AND 35 THEN '26-35岁'
WHEN a.age BETWEEN 36 AND 45 THEN '36-45岁'
WHEN a.age BETWEEN 45 AND 60 THEN '45-60岁'
WHEN a.age >= 60 THEN '60岁以上'
END) AS s,
COUNT(*) 'value' FROM
(SELECT(YEAR (curdate())- YEAR ( birthday )- 1+ (DATE_FORMAT( birthday, '%m%d' )<=(DATE_FORMAT( curdate(), '%m%d' )))) age FROM tt_person) a GROUP BY s;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值