MySQL 在Mysql中使用id从表中删除多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15504020/
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
Delete many rows from a table using id in Mysql
提问by Kevin Parker
I am a Linux admin with only basic knowledge in Mysql Queries
我是一名 Linux 管理员,对 Mysql 查询只有基本的了解
I want to delete many table entries which are ip address from my table using id,
我想使用id从我的表中删除许多作为 ip 地址的表条目,
currently i am using
目前我正在使用
DELETE from tablename where id=1;
DELETE from tablename where id=2;
but i have to delete 254 entries,so this method is going to take hours,how can i tell mysql to delete rows that i specify,coz i want to skip deleting some entries out of this 254.
但我必须删除 254 个条目,所以这个方法需要几个小时,我怎么能告诉 mysql 删除我指定的行,因为我想跳过从这 254 个中删除一些条目。
Deleting whole table and importing needed entries is not an option.
删除整个表并导入所需的条目不是一种选择。
回答by JoDev
The best way is to use IN
statement :
最好的方法是使用IN
statement :
DELETE from tablename WHERE id IN (1,2,3,...,254);
You can also use BETWEEN
if you have consecutive IDs :
BETWEEN
如果您有连续的 ID,您也可以使用:
DELETE from tablename WHERE id BETWEEN 1 AND 254;
You can of course limit for some IDs using other WHERE clause :
您当然可以使用其他 WHERE 子句限制某些 ID:
DELETE from tablename WHERE id BETWEEN 1 AND 254 AND id<>10;
回答by John Woo
how about using IN
怎么样使用 IN
DELETE FROM tableName
WHERE ID IN (1,2) -- add as many ID as you want.
回答by Ghigo
if you need to keep only a few rows, consider
如果您只需要保留几行,请考虑
DELETE FROM tablename WHERE id NOT IN (5,124,221);
This will keep only some records and discard others.
这将只保留一些记录并丢弃其他记录。
回答by cowls
Something like this might make it a bit easier, you could obviously use a script to generate this, or even excel
像这样的事情可能会更容易一些,您显然可以使用脚本来生成它,甚至是excel
DELETE FROM tablename WHERE id IN (
1,
2,
3,
4,
5,
6
);
回答by MuhammadHani
Use IN
Clause
使用IN
条款
DELETE from tablename where id IN (1,2);
OR you can merge the use of BETWEEN
and NOT IN
to decrease the numbers you have to mention.
或者,您可以合并使用BETWEEN
和NOT IN
减少您必须提及的数字。
DELETE from tablename
where (id BETWEEN 1 AND 255)
AND (id NOT IN (254));
回答by Boris the Spider
Others have suggested IN
, this is fine. You can also use a range:
其他人建议IN
,这很好。您还可以使用范围:
DELETE from tablename where id<254 and id>3;
If the ids to delete are contiguous.
如果要删除的 ID 是连续的。
回答by Vivek Todi
If you have some 'condition' in your data to figure out the 254 ids, you could use:
如果您的数据中有一些“条件”来计算 254 个 ID,您可以使用:
delete from tablename
where id in
(select id from tablename where <your-condition>)
or simply:
或者干脆:
delete from tablename where <your-condition>
Simply hard coding the 254 values of id column would be very tough in any case.
在任何情况下,简单地对 id 列的 254 个值进行硬编码都是非常困难的。
回答by sameer kumar
DELETE FROM table_name WHERE id BETWEEN 1 AND 256;
Try This.
尝试这个。
回答by mangrove108
Hope it helps:
希望能帮助到你:
DELETE FROM tablename
WHERE tablename.id = ANY (SELECT id FROM tablename WHERE id = id);