MySQL remove duplicate rows

后端 未结 1 1226
一向
一向 2021-01-06 10:41

I have a comments table, whose structure is as:

id, name, email, comment

I have many duplicate comments, with same name and email. I need t

相关标签:
1条回答
  • 2021-01-06 11:06
    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
    )
    
    0 讨论(0)
提交回复
热议问题