简体   繁体   中英

How to get customers age from MySQL Date

birthday
------------
1988-10-09
1919-12-12

How to calculate the age properly and rounded?

I have this:

SELECT
ROUND((TO_DAYS(NOW()) - TO_DAYS(birthday)) / 365)
FROM customer

Is there something more accurate?

SELECT TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age
FROM customer

Source

SQLFiddle demo

You can use datediff() function to get day count between two dates. Flooring the result gives you the integer part of the age.

SELECT FLOOR(DATEDIFF(CURDATE(),birthday) / 365) as customer_age
FROM costumer

这是另一个选择:

 SELECT ROUND(DATE_DIFF(NOW(), birthday) / 365) AS age FROM customer
SELECT TIMESTAMPDIFF(YEAR,birthday,NOW()) AS age FROM customer;

This would be helpful for you

select DATEDIFF(customer.dob, '2013-10-01') / 365.25 as age

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(customer.dob,'2013-10-01')), ‘%Y’)+0 AS age

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