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;
在這裏插入圖片描述

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