Remove duplicate rows in MySQL

前端 未结 25 3494
囚心锁ツ
囚心锁ツ 2020-11-21 04:33

I have a table with the following fields:

id (Unique)
url (Unique)
title
company
site_id

Now, I need to remove rows having same titl

25条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 05:31

    if you have a large table with huge number of records then above solutions will not work or take too much time. Then we have a different solution

    -- Create temporary table
    
    CREATE TABLE temp_table LIKE table1;
    
    -- Add constraint
    ALTER TABLE temp_table ADD UNIQUE(title, company,site_id);
    
    -- Copy data
    INSERT IGNORE INTO temp_table SELECT * FROM table1;
    
    -- Rename and drop
    RENAME TABLE table1 TO old_table1, temp_table TO table1;
    DROP TABLE old_table1;
    

提交回复
热议问题