MySQL 如何以动态方式删除第一组或最后一组行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8754931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:16:30  来源:igfitidea点击:

How do I delete either the first or last set of rows in a dynamic fashion

mysqlsql

提问by priya

I would like to delete the first 100 rows or the last 100 rows in a certain table (ordered by the primary key).

我想删除某个表中的前 100 行或最后 100 行(按主键排序)。

Note: Lots of data is being spooled into this table.

注意:大量数据被假脱机到这个表中。

回答by Itay Moav -Malimovka

DELETE FROM table ORDER BY the field DESC|ASC limit 100

回答by Bhavesh Gangani

for first 100,

前 100 名,

DELETE FROM table ORDER BY <field> ASC limit 100

and for last 100,

最后 100 次,

DELETE FROM table ORDER BY <field> DESC limit 100

回答by gbn

SET @first = 1;
delete from mytable 
where primKey in (select 1 
                  from myTable 
                  order by 
                    CASE WHEN @first = 1 THEN primKey END ASC,
                    CASE WHEN @first <> 1 THEN primKey END DESC
                  limit 100)