How to delete duplicates on a MySQL table?

后端 未结 25 2395
遇见更好的自我
遇见更好的自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:57

    Here is a simple answer:

    delete a from target_table a left JOIN (select max(id_field) as id, field_being_repeated  
        from target_table GROUP BY field_being_repeated) b 
        on a.field_being_repeated = b.field_being_repeated
          and a.id_field = b.id_field
        where b.id_field is null;
    

提交回复
热议问题