sql 两个时间相减 根据生日获取年龄

根据生日获取年龄

select birthday,DATEDIFF(now(),birthday)/365 age FROM lg_lawyer_base where not ISNULL(birthday) and isDeleted = 0;
在这里插入图片描述

使用当前DATEDIFF函数 用当前时间NOW()减去生日birthday除以365,即可获取到年龄

根据获取到的年龄分组统计

24以下、25到35、35到45、45到55、55到65、65以上

SELECT
count(age or null) count,
count((age < 24) or null) ageCount1,
count((age >= 25 and age <= 35) or null) ageCount2 ,
count((age >= 36 and age <= 45) or null) ageCount3 ,
count((age >= 46 and age <= 55) or null) ageCount4 ,
count((age >= 56 and age <= 65) or null) ageCount5 ,
count((age >= 66) or null) ageCount6
FROM
(select birthday,DATEDIFF(now(),birthday)/365 age FROM lg_lawyer_base where not ISNULL(birthday) and isDeleted = 0) ageTable;
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章