1 函数 datediff(返回值是相差的天数)
select datediff('2020-03-22 22:00:00', '2020-03-23 01:00:00') as day01,
datediff('2020-03-25 22:00:00', '2020-03-23 01:00:00') as day02
执行结果:
2 函数 timestampdiff(有参数设置,返回值可以精确到天、小时,分钟和秒)
-- 4天
select timestampdiff(day, '2020-04-20 20:00:00', '2020-04-25 01:00:00') as day_num
;
-- 1小时
select timestampdiff(hour, '2020-04-20 10:30:00', '2020-04-20 12:04:00') as hour_num
;
;
-- 19分钟
select timestampdiff(minute, '2020-04-20 09:50:50', '2020-04-20 10:10:10') as minute_num
;
-- 75秒
select timestampdiff(second, '2020-03-20 09:00:00', '2020-03-20 09:01:15') as second_num
;