How to delete completely duplicate rows

后端 未结 4 1691
孤街浪徒
孤街浪徒 2021-01-18 05:31

Say i have duplicate rows in my table and well my database design is of 3rd class :-

Insert Into tblProduct (ProductId,ProductName,Description,Category) Valu         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 05:46

    DELETE tblProduct 
    FROM tblProduct 
    LEFT OUTER JOIN (
       SELECT MIN(ProductId) as ProductId, ProductName, Description, Category
       FROM tblProduct 
       GROUP BY ProductName, Description, Category
    ) as KeepRows ON
       tblProduct.ProductId= KeepRows.ProductId
    WHERE
       KeepRows.ProductId IS NULL
    

    Stolen from How can I remove duplicate rows?

    UPDATE:

    This will only work if ProductId is a Primary Key (which it is not). You are better off using @marc_s' method, but I'll leave this up in case someone using a PK comes across this post.

提交回复
热议问题