How to delete duplicates on a MySQL table?

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

    Deleting duplicates on MySQL tables is a common issue, that usually comes with specific needs. In case anyone is interested, here (Remove duplicate rows in MySQL) I explain how to use a temporary table to delete MySQL duplicates in a reliable and fast way, also valid to handle big data sources (with examples for different use cases).

    Ali, in your case, you can run something like this:

    -- create a new temporary table
    CREATE TABLE tmp_table1 LIKE table1;
    
    -- add a unique constraint    
    ALTER TABLE tmp_table1 ADD UNIQUE(sid, title);
    
    -- scan over the table to insert entries
    INSERT IGNORE INTO tmp_table1 SELECT * FROM table1 ORDER BY sid;
    
    -- rename tables
    RENAME TABLE table1 TO backup_table1, tmp_table1 TO table1;
    

提交回复
热议问题