我有一张桌子:
| foo | bar |
+-----+-----+
| a | abc |
| b | def |
| c | ghi |
| d | jkl |
| a | mno |
| e | pqr |
| c | stu |
| f | vwx |
我想删除foo
列中包含重复项的所有行,以便表格如下所示:
| foo | bar |
+-----+-----+
| b | def |
| d | jkl |
| e | pqr |
| f | vwx |
最有效的方式是什么?
答案 0 :(得分:9)
您可以从子查询加入表,该子查询仅使用foo
返回唯一LEFT JOIN
。子查询上没有匹配的行将根据需要删除,例如
DELETE a
FROM TableName a
LEFT JOIN
(
SELECT foo
FROM TableName
GROUP BY Foo
HAVING COUNT(*) = 1
) b ON a.Foo = b.Foo
WHERE b.Foo IS NULL
为了提高性能,请在列Foo
上添加索引。
ALTER TABLE tableName ADD INDEX(foo)
答案 1 :(得分:8)
使用EXISTS
:
DELETE a
FROM TableName a
WHERE EXISTS (SELECT NULL
FROM TableName b
WHERE b.foo = a.foo
GROUP BY b.foo
HAVING COUNT(*) > 1)
使用IN
:
DELETE a
FROM TableName a
WHERE a.foo IN (SELECT b.foo
FROM TableName b
GROUP BY b.foo
HAVING COUNT(*) > 1)