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

如何正确计算年龄和四舍五入?

我有这个:

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

有更准确的吗?

4

4 回答 4

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

资源

SQLFiddle 演示

于 2013-10-01T07:35:01.387 回答
0

您可以使用datediff()函数来获取两个日期之间的天数。对结果进行地板化可以为您提供年龄的整数部分。

SELECT FLOOR(DATEDIFF(CURDATE(),birthday) / 365) as customer_age
FROM costumer
于 2013-10-01T07:35:29.913 回答
0

这是另一种选择:

 SELECT ROUND(DATE_DIFF(NOW(), birthday) / 365) AS age FROM customer
于 2013-10-01T07:35:50.120 回答
0

这对你有帮助

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
于 2013-10-01T10:36:44.853 回答