MySQL 删除数据库的前 X 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12779978/
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 15:08:06  来源:igfitidea点击:

Delete first X lines of a database

mysqlsql-delete

提问by coroner

is there an SQL command to delete the first X lines of a database table?

是否有删除数据库表的前 X 行的 SQL 命令?

I have a database table containing some information but no id or auto-incrementing value and a program that processes the first X lines of this table. Afterwards these X lines need to be deleted. So the standard query is:

我有一个包含一些信息但没有 id 或自动递增值的数据库表和一个处理该表前 X 行的程序。之后需要删除这些 X 行。所以标准查询是:

DELETE FROM table WHERE something = value;

So, is there a way to build a query like:

那么,有没有办法构建一个查询,如:

DELETE FROM table WHERE rownumber <= X;

I have tried this command, but nothing happens to the database.. Do you have any clue?

我试过这个命令,但数据库没有任何反应..你有什么线索吗?

回答by Polynomial

Use LIMITon your delete:

LIMIT在删除时使用:

DELETE FROM table WHERE condition LIMIT 10

Or, if you don't want the condition

或者,如果你不想要条件

DELETE FROM table LIMIT 10

Remember that the order in which rows will be deleted is undefined - it depends on your DBMS configuration and table indices. You should include an ORDER BYso that the deletion is done in a defined order, e.g. ORDER BY id ASCto delete the lowest IDs first.

请记住,删除行的顺序是未定义的 - 这取决于您的 DBMS 配置和表索引。您应该包含 ,ORDER BY以便按定义的顺序完成ORDER BY id ASC删除,例如首先删除最低的 ID。

See the MySQL documentation for DELETEfor more details.

有关详细信息,请参阅DELETEMySQL 文档