小编典典

MySQL删除重复的行

sql

我有一个注释表,其结构如下:

id, name, email, comment

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

谢谢


阅读 181

收藏
2021-03-23

共1个答案

小编典典

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
)
2021-03-23