简体   繁体   中英

How to calculate age based on date of birth in mysql?

My date of birth format is : 2017-Feb-15

when I am using the following query, it's not working

select (year(current_date)-year(dob)) as age from user 

or how to current 2017-Feb-15 into 2017-02-15 format

You can use TIMESTAMPDIFF .

select TIMESTAMPDIFF(YEAR, str_to_date(dob, '%Y-%M-%d'), current_date) AS age
from user;

you can try this code.

select year(CURDATE())- year(dob) as age from user;

CURDATE() function meant current date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM