第一种
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;