How to delete duplicates on a MySQL table?

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

    I think this will work by basically copying the table and emptying it then putting only the distinct values back into it but please double check it before doing it on large amounts of data.

    Creates a carbon copy of your table

    create table temp_table like oldtablename; insert temp_table select * from oldtablename;

    Empties your original table

    DELETE * from oldtablename;

    Copies all distinct values from the copied table back to your original table

    INSERT oldtablename SELECT * from temp_table group by firstname,lastname,dob

    Deletes your temp table.

    Drop Table temp_table

    You need to group by aLL fields that you want to keep distinct.

提交回复
热议问题