题解 | #删除记录(一)#
删除记录(一)
https://www.nowcoder.com/practice/d331359c5ca04a3b87f06b97da42159c
delete from exam_record where timestampdiff(minute,start_time,submit_time)<5 and score<60;
方法一、日期减去日期得到分钟
timestampdiff函数 timestampdiff(minute, begin, end) < 5
方法二、日期减去(加上)分钟得到日期
date_sub函数 date_sub(end, interval 5 minute ) < begin
delete from exam_record
where date_sub(submit_time, interval 5minute ) < start_time
and score < 60
date_add函数 date_add(begin, interval 5 minute ) > end
delete from exam_record
where date_add(start_time, interval 5minute ) > submit_time
为什么不能用datediff()函数???
--------------------------------------------------------------------------------------------------------------------------------------
MySQL的时间差函数TIMESTAMPDIFF、DATEDIFF的用法
datediff函数,返回值是相差的天数,不能定位到小时、分钟和秒。
-- 相差2天
select datediff('2018-03-22 09:00:00', '2018-03-20 07:00:00');
TIMESTAMPDIFF函数,有参数设置,可以精确到天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND),使用起来比datediff函数更加灵活。对于比较的两个时间,时间小的放在前面,时间大的放在后面。
--相差1天
select TIMESTAMPDIFF(DAY, '2018-03-20 23:59:00', '2015-03-22 00:00:00');
--相差49小时
select TIMESTAMPDIFF(HOUR, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
--相差2940分钟
select TIMESTAMPDIFF(MINUTE, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
--相差176400秒
select TIMESTAMPDIFF(SECOND, '2018-03-20 09:00:00', '2018-03-22 10:00:00');