简体   繁体   English

如何从MySQL Date获得客户年龄

[英]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 SQLFiddle演示

You can use datediff() function to get day count between two dates. 您可以使用datediff()函数获取两个日期之间的天数。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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