How to delete duplicates on a MySQL table?

后端 未结 25 2437
遇见更好的自我
遇见更好的自我 2020-11-22 01:35

I need to DELETE duplicated rows for specified sid on a MySQL table.

How can I do this with an SQL query?

         


        
25条回答
  •  失恋的感觉
    2020-11-22 01:56

    If you want to keep the row with the lowest id value:

     DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id > n2.id AND n1.email = n2.email
    

    If you want to keep the row with the highest id value:

     DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id < n2.id AND n1.email = n2.email
    

提交回复
热议问题