MySQL删除重复的行

时间:2011-01-17 11:52:11

标签: mysql sql

我有一个评论表,其结构如下:

id, name, email, comment

我有很多重复的评论,同名和电子邮件。我需要删除它们,任何人都可以建议我如何使用单个查询实现此目的?

由于

1 个答案:

答案 0 :(得分:7)

DELETE FROM comments c1 

WHERE EXISTS (
    SELECT * 
    FROM comments c2 
    WHERE c2.id <> c1.id 
        AND c2.name = c1.name 
        AND c2.email = c1.email 
        AND c2.comment = c1.comment
)

AND c1.id <> (
    SELECT MIN(c2.id) 
    FROM comments c2 
    WHERE c2.name = c1.name 
        AND c2.email = c1.email 
        AND c2.comment = c1.comment
)