mysql查询不同年龄段的人数

mysql查询不同年龄段的人数:

数据库中存入的出生日期birth格式为20170101或者2017-01-01都行

SELECT SUM(if(((year(now())-year(birth)-1) + ( DATE_FORMAT(birth, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=10),1,0)) AS result1,
SUM(if(((year(now())-year(birth)-1) + ( DATE_FORMAT(birth, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=30),1,0)) AS result2,
SUM(if(((year(now())-year(birth)-1) + ( DATE_FORMAT(birth, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=60),1,0)) AS result3
FROM table_age;


你可能感兴趣的:(数据库)