What is the simplest way to delete records with duplicate name in a table? The answers I came across are very confusing.
问问题
2741 次
2 回答
5
我知道了!简单而且效果很好。
delete
t1
from
tTable t1, tTable t2
where
t1.locationName = t2.locationName and
t1.id > t2.id
于 2009-05-08T19:40:52.843 回答
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
于 2009-05-08T19:44:43.380 回答