SELECT FLOOR(DATEDIFF(CURDATE(), STR_TO_DATE(birthday, '%Y-%m-%d'))/365) AS age FROM table_name
拓展:MySQL中可以使用TIMESTAMPDIFF函数根据出生日期计算年龄,它接受5个参数:
TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2, [datetime_expr3], [datetime_expr4])
unit参数可以是YEAR,QUARTER,MONTH,WEEK,DAY,HOUR,MINUTE,SECOND中的任何一个,代表了计算结果的单位。
在Mysql中计算年龄:SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age FROM 表名
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)