Delete duplicate rows

前端 未结 3 1608
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 14:55

I have a table that looks like this

Table1

Id, Name

How can I write a query that delete all rows with duplicate names but kee

3条回答
  •  花落未央
    2021-01-14 15:30

    The duplicates can be removed with a simple self join query. The below script would do the trick for you.

    delete t2
    from Table1 t1
    join Table1 t2
       on t1.Name = t2.Name
    where t1.Id < t2.Id
    

    This logic can be used for cases where duplicates need to be removed. We should avoid "cursor" as much as possible as it blocks the table.

提交回复
热议问题