DATEDIFF函数

只可以计算天数

SQL语句:

1
SELECT DATEDIFF ("2021-04-25 23:23:12","2021-04-21 23:23:12") AS days;

结果:

img

TIMESTAMPDIFF函数

可以根据自己的需求计算天数、小时、分钟、秒钟

SQL语句:

1
2
3
4
SELECT TIMESTAMPDIFF (DAY,"2021-04-21 23:23:12","2021-04-25 23:23:12") AS "天";
SELECT TIMESTAMPDIFF (HOUR,"2021-04-21 23:23:12","2021-04-25 23:23:12") AS "时";
SELECT TIMESTAMPDIFF (MINUTE,"2021-04-21 23:23:12","2021-04-25 23:23:12") AS "分";
SELECT TIMESTAMPDIFF (SECOND,"2021-04-21 23:23:12","2021-04-25 23:23:12") AS "秒";

结果:(图片从上到下与上面的SQL语句从上到下一一对应)

imgimgimgimg

PS.

来源:mysql 比较两个日期的时间差 - 未确定 - 博客园