SqlServer删除重复数据保留一条

1、查询出重复的数据,Id只保留其中一个

SELECT MIN(Id) Id,Name FROM dbo.City 
GROUP BY Name
HAVING COUNT(1) >  1

2、然后使用删除时Join上面的表

DELETE C FROM City C 
JOIN (
    SELECT MIN(Id) Id,Name FROM dbo.City
    GROUP BY Name
    HAVING COUNT(1) >  1
) TMP ON C.Name = TMP.Name AND C.Id <> TMP.Id
 
posted @ 2022-07-22 14:39  随便取个名字算了  阅读(3337)  评论(0编辑  收藏  举报