1. 除最新n条数据之外,其他数据均删除
delete from expire_prompt where id not in (select id from (select id from expire_prompt ORDER BY id desc limit #{saveRecordsNum}) as t1)
多加个select的原因是内层select语句不能带有limit语句,只能把它看作表来查,记得加别名才行。
本文介绍了一种使用SQL语句高效清理数据的方法,通过一个特定的SQL查询,可以实现保留最新的n条记录,同时删除其余所有旧数据。这种方法适用于需要定期更新数据集并保持最新状态的应用场景。
delete from expire_prompt where id not in (select id from (select id from expire_prompt ORDER BY id desc limit #{saveRecordsNum}) as t1)
多加个select的原因是内层select语句不能带有limit语句,只能把它看作表来查,记得加别名才行。
2636
1997
7639

被折叠的 条评论
为什么被折叠?