How to delete duplicates in SQL table based on multiple fields

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

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

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


        
9条回答
  •  北海茫月
    2020-12-04 15:24

    To get list of duplicate entried matching two fields

    select t.ID, t.field1, t.field2
    from (
      select field1, field2
      from table_name
      group by field1, field2
      having count(*) > 1) x, table_name t
    where x.field1 = t.field1 and x.field2 = t.field2
    order by t.field1, t.field2
    

    And to delete all the duplicate only

    DELETE x 
    FROM table_name x
    JOIN table_name y
    ON y.field1= x.field1
    AND y.field2 = x.field2
    AND y.id < x.id;
    

提交回复
热议问题