how to delete duplicates from a database table based on a certain field

前端 未结 2 597
半阙折子戏
半阙折子戏 2020-12-22 02:38

i have a table that somehow got duplicated. i basically want to delete all records that are duplicates, which is defined by a field in my table called SourceId. There shou

相关标签:
2条回答
  • 2020-12-22 03:06

    Assuming you have a column ID that can tie-break the duplicate sourceid's, you can use this. Using min(id) causes it to keep just the min(id) per sourceid batch.

    delete from tbl
    where id NOT in
    (
    select  min(id)
    from tbl
    group by sourceid
    )
    
    0 讨论(0)
  • 2020-12-22 03:20
    delete from table
    where pk in (
    select i2.pk
    from table i1
      inner join table i2
       on i1.SourceId = i2.SourceId
    )
    

    good practice is to start with select * from … and only later replace to delete from …

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