问题描述
是否有SQL命令删除数据库表的前X行?
is there an SQL command to delete the first X lines of a database table?
我有一个数据库表,其中包含一些信息,但没有id或自动递增值,还有一个程序处理该表的前X行.之后,需要删除这些X行.所以标准查询是:
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:
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?
推荐答案
在删除时使用LIMIT
:
DELETE FROM table WHERE condition LIMIT 10
或者,如果您不希望出现这种情况
Or, if you don't want the condition
DELETE FROM table LIMIT 10
请记住,删除行的顺序是不确定的-这取决于您的DBMS配置和表索引.您应包括ORDER BY
,以便按定义的顺序删除文件,例如ORDER BY id ASC
首先删除最低的ID.
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 BY
so that the deletion is done in a defined order, e.g. ORDER BY id ASC
to delete the lowest IDs first.
有关更多详细信息,请参见有关DELETE的MySQL文档.
See the MySQL documentation for DELETE for more details.
这篇关于删除数据库的前X行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!