How to delete duplicates on a MySQL table?

后端 未结 25 2930
遇见更好的自我
遇见更好的自我 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 02:04

    here is how I usually eliminate duplicates

    1. add a temporary column, name it whatever you want(i'll refer as active)
    2. group by the fields that you think shouldn't be duplicate and set their active to 1, grouping by will select only one of duplicate values(will not select duplicates)for that columns
    3. delete the ones with active zero
    4. drop column active
    5. optionally(if fits to your purposes), add unique index for those columns to not have duplicates again

提交回复
热议问题