简体   繁体   English

使用Mysql中的id从表中删除许多行

[英]Delete many rows from a table using id in Mysql

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. 删除整个表并导入所需的条目不是一种选择。

The best way is to use IN statement : 最好的方法是使用IN语句:

DELETE from tablename WHERE id IN (1,2,3,...,254);

You can also use BETWEEN if you have consecutive IDs : 如果您有连续的ID,也可以使用BETWEEN

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;

how about using IN 如何使用IN

DELETE FROM tableName
WHERE ID IN (1,2) -- add as many ID as you want.

Something like this might make it a bit easier, you could obviously use a script to generate this, or even excel 像这样的东西可能会让它变得更容易一些,你显然可以使用脚本来生成它,甚至可以表现出色

DELETE FROM tablename WHERE id IN (
1,
2,
3,
4,
5,
6
);

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. 这将仅保留一些记录并丢弃其他记录。

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. 或者您可以合并使用BETWEENNOT IN来减少您必须提及的数字。

DELETE from tablename 
where (id BETWEEN 1 AND 255) 
AND (id NOT IN (254));

Others have suggested IN , this is fine. 其他人建议IN ,这很好。 You can also use a range: 您还可以使用范围:

DELETE from tablename where id<254 and id>3;

If the ids to delete are contiguous. 如果要删除的ID是连续的。

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值进行硬编码将非常困难。

DELETE FROM table_name WHERE id BETWEEN 1 AND 256;

试试这个。

Hope it helps: 希望能帮助到你:

DELETE FROM tablename 
WHERE tablename.id = ANY (SELECT id FROM tablename WHERE id = id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM