How to delete duplicates in SQL table based on multiple fields

后端 未结 9 449
星月不相逢
星月不相逢 2020-12-04 14:58

I have a table of games, which is described as follows:

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type                


        
相关标签:
9条回答
  • 2020-12-04 15:35
    DELETE FROM table
    WHERE id = 
        (SELECT t.id
        FROM table as t
        JOIN (table as tj ON (t.date = tj.data
                              AND t.hometeam_id = tj.hometeam_id
                              AND t.awayteam_id = tj.awayteam_id
                              ...))
    
    0 讨论(0)
  • 2020-12-04 15:36
    delete from games 
       where id not in 
       (select max(id)  from games 
        group by date, time, hometeam_id, awayteam_id, locationcity, locationstate 
        );
    

    Workaround

    select max(id)  id from games 
        group by date, time, hometeam_id, awayteam_id, locationcity, locationstate
    into table temp_table;
    
    delete from games where id in (select id from temp);
    
    0 讨论(0)
  • 2020-12-04 15:40

    The best thing that worked for me was to recreate the table.

    CREATE TABLE newtable SELECT * FROM oldtable GROUP BY field1,field2;
    

    You can then rename.

    0 讨论(0)
提交回复
热议问题