mysql两个日期之间
时间: 2024-02-06 18:02:28 浏览: 88
输出两个日期之间的日期
4星 · 用户满意度95%
在MySQL中,可以使用TimestampDiff()函数来计算两个日期之间的差异。该函数返回日期或日期时间表达式datetime_expr1和datetime_expr2之间的整数差,单位可以是秒、分钟、小时、天、周、月、季度或年。
以下是使用TimestampDiff()函数计算两个日期之间差异的示例代码:
SELECT TIMESTAMPDIFF(SECOND, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS seconds_diff;
SELECT TIMESTAMPDIFF(MINUTE, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS minutes_diff;
SELECT TIMESTAMPDIFF(HOUR, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS hours_diff;
SELECT TIMESTAMPDIFF(DAY, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS days_diff;
SELECT TIMESTAMPDIFF(WEEK, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS weeks_diff;
SELECT TIMESTAMPDIFF(MONTH, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS months_diff;
SELECT TIMESTAMPDIFF(YEAR, '2022-01-01 00:00:00', '2022-01-02 12:00:00') AS years_diff;
以上代码将返回两个日期之间的差异,分别以秒、分钟、小时、天、周、月和年为单位。
阅读全文