SoFunction
Updated on 2025-03-03

MySQL implements the method of calculating age through birthday

MySQL calculates age by birthday (same thing, you can calculate age by 18-digit ID number)
– The birthday type is String yyyy-mm-dd. The age is calculated based on 365 days a year.

SELECT
  birthday,
  FLOOR(PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m%d'), DATE_FORMAT(birthday, '%Y%m%d'))/365) AS age
FROM
  persons;

– The birthday type is Date

SELECT
  birthday,
  TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age
FROM
  persons;

– The birthday type is String yyyy-mm Age is calculated based on 12 months of a year

SELECT
  ,
	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  = 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));

This is the article about mysql calculating age through birthdays. For more related content about mysql calculating age through birthdays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!