mysql删除旧数据,保留最新的m条记录

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/85717138

sql如下:

select   *  from  area_table ORDER BY id limit 1670,1  -- 结果id=1671 1条记录

select   *  from  area_table ORDER BY id limit 10 -- 结果id = 1...10 前10条记录

delete from area_table where id <
(select   id  from  area_table ORDER BY id desc limit 1670,1)
-- 错误
--  You can't specify target table 'area_table' for update in FROM clause

delete from area_table where id  not in (
select   id   from  area_table ORDER BY id desc limit 1000,1
) -- 错误
-- [Err] 1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

delete from area_table a ,(select   id   from  area_table ORDER BY id desc limit 1000,1) b
where a.id<b.id -- 错误

delete a from area_table a join (select   id   from  area_table ORDER BY id desc limit 901,1) b
on a.id<b.id  -- 正常

delete a from area_table a ,(select   id   from  area_table ORDER BY id desc limit 2000,1) b
where a.id<b.id -- 正常,没有2000条数据时不删除

delete a from area_table a ,(select  DISTINCT id   from  area_table ORDER BY id desc limit 900,1) b
where a.id<b.id -- 正常

版本:5.7.14

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/85717138