1.利用TO_DAYS函数
select to_days(now()) - to_days('20120512')
2.利用DATEDIFF函数
select datediff(now(),'20120512')
TIMESTAMPDIFF函数
可用于计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数
语法:TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
unit单位有如下几种:FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR
-例如:
计算两日期之间相差多少秒
select timestampdiff(SECOND,'1997-10-12','2022-01-10');
计算两日期之间相差多少天
select timestampdiff(DAY,'1997-10-12','2022-01-10');
计算工龄
SELECT TIMESTAMPDIFF(YEAR,'2019-07-20',NOW()) AS 'working years';