删除重复的SQL记录

时间:2009-05-08 19:37:51

标签: sql duplicates

删除表中具有重复名称的记录的最简单方法是什么?我遇到的答案非常令人困惑。

相关:

  

Removing duplicate records from table

2 个答案:

答案 0 :(得分:5)

我明白了!简单而且效果很好。

delete 
   t1 
from 
   tTable t1, tTable t2 
where 
   t1.locationName = t2.locationName and  
   t1.id > t2.id 

http://www.cryer.co.uk/brian/sql/sql_delete_duplicates.htm

答案 1 :(得分:0)

SQL Server 2005:

with FirstKey
AS
(
    SELECT MIN(ID), Name, COUNT(*) AS Cnt
      FROM YourTable
     GROUP BY Name
     HAVING COUNT(*) > 1
)
DELETE YourTable
  FROM YourTable YT
  JOIN FirstKey FK ON FK.Name = YT.Name AND FK.ID != YT.ID