1

我有一张表,我需要正确的语句来删除重复记录。我想通过删除表中多个列值等于同一个表中其他记录的行来实现这一点。我可以做的很简单delete from [table] where [col] = [val],但我不知道该怎么做。你能提供一个我可以使用的模板吗?

谢谢。

4

2 回答 2

1
delete from [table] 
where [col] not in 
(
  select * from 
  (
    select min([col])
    from [table]
    group by [col]
    having count(distinct [val]) <> count([val])
  ) x
)

如果您从 MySQL 中要删除的表中进行选择,则必须使用子查询来隐藏它。这就是为什么我使用select * from (...) x

于 2013-06-17T16:01:25.717 回答
0

假设原始密钥的 id 是 say 7

DELETE FROM [table] WHERE [col1] = [val1] AND [col2] = [val2] .... AND [id] != 7;
于 2013-06-17T16:01:53.900 回答