How to remove duplicate entries from a mysql db?

后端 未结 8 1297
南旧
南旧 2020-12-02 10:41

I have a table with some ids + titles. I want to make the title column unique, but it has over 600k records already, some of which are duplicates (sometimes several dozen ti

相关标签:
8条回答
  • 2020-12-02 11:07

    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 (with examples for different use cases).

    In this case, something like this should work:

    -- create a new temporary table
    CREATE TABLE tmp_table1 LIKE table1;
    
    -- add a unique constraint    
    ALTER TABLE tmp_table1 ADD UNIQUE(id, 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;
    
    0 讨论(0)
  • 2020-12-02 11:16

    Below query can be used to delete all the duplicate except the one row with lowest "id" field value

    DELETE t1 FROM table_name t1, table_name t2 WHERE t1.id > t2.id AND t1.name = t2.name
    

    In the similar way, we can keep the row with the highest value in 'id' as follows

     DELETE t1 FROM table_name t1, table_name t2 WHERE t1.id < t2.id AND t1.name = t2.name
    
    0 讨论(0)
提交回复
热议问题