birthday
------------
1988-10-09
1919-12-12
如何正确计算年龄和舍入?
我有这个:
SELECT
ROUND((TO_DAYS(NOW()) - TO_DAYS(birthday)) / 365)
FROM customer
有更准确的东西吗?
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以使用datediff()
功能获取两个日期之间的日期。对结果进行地板化可以得到年龄的整数部分。
SELECT FLOOR(DATEDIFF(CURDATE(),birthday) / 365) as customer_age
FROM costumer
答案 2 :(得分:0)
这是另一个选择:
SELECT ROUND(DATE_DIFF(NOW(), birthday) / 365) AS age FROM customer
答案 3 :(得分:0)
SELECT TIMESTAMPDIFF(YEAR,birthday,NOW()) AS age FROM customer;
答案 4 :(得分: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