Mysql根据出生日期查询年龄,并分组

@Mysql

Mysql根据出生日期查询年龄,并分组

新的改变

根据生日查询出生日期:

SELECT(YEAR (curdate())- YEAR ( birthday )- 1+ (DATE_FORMAT( birthday, ‘%m%d’ )<=(DATE_FORMAT( curdate(), ‘%m%d’ )))) age FROM t_member

分组

SELECT (CASE
WHEN a.age IS NULL THEN ‘未知’
WHEN a.age BETWEEN 0 AND 18 THEN ‘0-18岁’
WHEN a.age BETWEEN 18 AND 30 THEN ‘18-30岁’
WHEN a.age BETWEEN 31 AND 45 THEN ‘30-45岁’
WHEN a.age >= 45 THEN ‘45岁以上’
END) AS name,
COUNT(*) ‘value’ FROM
(SELECT(YEAR (curdate())- YEAR ( birthday )- 1+ (DATE_FORMAT( birthday, ‘%m%d’ )<=(DATE_FORMAT( curdate(), ‘%m%d’ )))) age FROM t_member) a GROUP BY name
Mysql根据出生日期查询年龄,并分组_第1张图片

查询结果:

Mysql根据出生日期查询年龄,并分组_第2张图片

你可能感兴趣的:(java)