首页 > 数据库 >MySQL计算两个日期的相差天数

MySQL计算两个日期的相差天数

时间:2023-08-02 11:35:45浏览次数:45  
标签:10 相差 天数 日期 TIMESTAMPDIFF SECOND MySQL select

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';


标签:10,相差,天数,日期,TIMESTAMPDIFF,SECOND,MySQL,select
From: https://blog.51cto.com/u_16111399/6935292

相关文章