mysql通過生日計算年齡的實現方法
更新時間:2024年11月07日 08:29:23 作者:janexu12
本文主要介紹了mysql通過生日計算年齡的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
mysql通過生日計算年齡(同理可得通過18位身份證號計算年齡)
– 生日類型是String yyyy-mm-dd 按一年365天計算年齡
SELECT birthday, FLOOR(PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m%d'), DATE_FORMAT(birthday, '%Y%m%d'))/365) AS age FROM persons;
– 生日類型是Date
SELECT birthday, TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age FROM persons;
– 生日類型是String yyyy-mm 按一年12個月計算年齡
SELECT m.sex, m.card_no, m.birth_date, FLOOR(PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'), DATE_FORMAT(CONCAT(m.birth_date,'-01'), '%Y%m'))/12) AS age FROM tb_cloud_member m LEFT JOIN tb_cloud_laborun l on l.laborun_code=m.laborun_code LEFT JOIN tb_cloud_enterprises tce ON tce.id = m.unit_id and l.is_deleted=0 and tce.is_deleted=0 where 1=1 and m.is_deleted=0 and l.is_laborun =1 and m.is_new_occupation=2 AND ( m.retirement_flag IS NULL OR m.retirement_flag = '' OR m.retirement_flag = '0' OR m.retirement_flag = '2' ) AND ( m.job_status IS NULL OR m.job_status not in (4,5,6));
到此這篇關于mysql通過生日計算年齡的文章就介紹到這了,更多相關mysql通過生日計算年齡內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
MySQL性能參數詳解之Max_connect_errors 使用介紹
這篇文章主要介紹了MySQL性能參數詳解之Max_connect_errors 使用介紹,需要的朋友可以參考下2016-05-05