birthday
------------
1988-10-09
1919-12-12
如何正确计算年龄和四舍五入?
我有这个:
SELECT
ROUND((TO_DAYS(NOW()) - TO_DAYS(birthday)) / 365)
FROM customer
有更准确的吗?
birthday
------------
1988-10-09
1919-12-12
如何正确计算年龄和四舍五入?
我有这个:
SELECT
ROUND((TO_DAYS(NOW()) - TO_DAYS(birthday)) / 365)
FROM customer
有更准确的吗?
您可以使用datediff()
函数来获取两个日期之间的天数。对结果进行地板化可以为您提供年龄的整数部分。
SELECT FLOOR(DATEDIFF(CURDATE(),birthday) / 365) as customer_age
FROM costumer
这是另一种选择:
SELECT ROUND(DATE_DIFF(NOW(), birthday) / 365) AS age FROM customer
这对你有帮助
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