我是一名Linux管理员,只掌握Mysql查询的基本知识
我想使用 id ,
删除我桌上ip地址的许多表条目目前我正在使用
DELETE from tablename where id=1;
DELETE from tablename where id=2;
但我必须删除254个条目,所以这个方法需要几个小时,我怎么能告诉mysql删除我指定的行,因为我想跳过删除254中的一些条目。
删除整个表并导入所需的条目不是一种选择。
答案 0 :(得分:141)
最好的方法是使用IN
声明:
DELETE from tablename WHERE id IN (1,2,3,...,254);
如果您有连续的ID,也可以使用BETWEEN
:
DELETE from tablename WHERE id BETWEEN 1 AND 254;
您当然可以使用其他WHERE子句限制某些ID:
DELETE from tablename WHERE id BETWEEN 1 AND 254 AND id<>10;
答案 1 :(得分:7)
如何使用IN
DELETE FROM tableName
WHERE ID IN (1,2) -- add as many ID as you want.
答案 2 :(得分:5)
这样的东西可能会让它变得更容易一点,显然你可以用脚本来生成它,甚至可以用excel
DELETE FROM tablename WHERE id IN (
1,
2,
3,
4,
5,
6
);
答案 3 :(得分:5)
如果您只需要保留几行,请考虑
DELETE FROM tablename WHERE id NOT IN (5,124,221);
这只保留一些记录并丢弃其他记录。
答案 4 :(得分:3)
使用IN
条款
DELETE from tablename where id IN (1,2);
或者您可以合并使用BETWEEN
和NOT IN
来减少您必须提及的数字。
DELETE from tablename
where (id BETWEEN 1 AND 255)
AND (id NOT IN (254));
答案 5 :(得分:3)
其他人建议IN
,这很好。您还可以使用范围:
DELETE from tablename where id<254 and id>3;
如果要删除的ID是连续的。
答案 6 :(得分:2)
如果您的数据中有某些'条件'来计算254个ID,则可以使用:
delete from tablename
where id in
(select id from tablename where <your-condition>)
或简单地说:
delete from tablename where <your-condition>
在任何情况下,只需对id列的254值进行硬编码就会非常困难。
答案 7 :(得分:1)
DELETE FROM table_name WHERE id BETWEEN 1 AND 256;
试试这个。
答案 8 :(得分:0)
希望有帮助:
DELETE FROM tablename
WHERE tablename.id = ANY (SELECT id FROM tablename WHERE id = id);
答案 9 :(得分:0)
DELETE FROM tablename WHERE id > 0;
OR
DELETE FROM tablename WHERE id <255;
删除 1 到 254 之间的 id