题解 116 | #删除记录(二)#
【场景】:删除前3条记录 【分类】:删除记录、日期函数
分析思路
难点:
1.如何取最早的三条记录
如何取最早的3条记录
升序排序后,删除前3条
-
[使用]:order by start_time limit 3
-
[注意]:在delete里面limit只能单参数,即 limit n,不能写成 limit n,m
方法一、日期减去日期得到分钟
timestampdiff函数
- [使用]:
timestampdiff(minute, begin, end) < 5
方法二、日期减去(加上)分钟得到日期
date_sub函数
- [使用]:
date_sub(end, interval 5 minute ) < begin
date_add函数
- [使用]:
date_add(begin, interval 5 minute ) > end
扩展
前往查看:MySQL 删除数据
求解代码
方法一:
timestampdiff
delete from exam_record
where timestampdiff(minute, start_time, submit_time) < 5
or submit_time is null
order by start_time
limit 3
方法二:
date_sub
delete from exam_record
where date_sub(submit_time, interval 5 minute ) < start_time
or submit_time is null
order by start_time
limit 3
方法三:
date_add
delete from exam_record
where date_add(start_time, interval 5 minute ) > submit_time
or submit_time is null
order by start_time
limit 3