How to delete duplicates on a MySQL table?

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

    This always seems to work for me:

    CREATE TABLE NoDupeTable LIKE DupeTable; 
    INSERT NoDupeTable SELECT * FROM DupeTable group by CommonField1,CommonFieldN;
    

    Which keeps the lowest ID on each of the dupes and the rest of the non-dupe records.

    I've also taken to doing the following so that the dupe issue no longer occurs after the removal:

    CREATE TABLE NoDupeTable LIKE DupeTable; 
    Alter table NoDupeTable Add Unique `Unique` (CommonField1,CommonField2);
    INSERT IGNORE NoDupeTable SELECT * FROM DupeTable;
    

    In other words, I create a duplicate of the first table, add a unique index on the fields I don't want duplicates of, and then do an Insert IGNORE which has the advantage of not failing as a normal Insert would the first time it tried to add a duplicate record based on the two fields and rather ignores any such records.

    Moving fwd it becomes impossible to create any duplicate records based on those two fields.

提交回复
热议问题