码迷,mamicode.com
首页 > 数据库 > 详细

MSSQL 删除重复并只保留一条

时间:2020-02-05 13:27:54      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:保留   mss   sql   product   having   field   ati   sel   version   

重复值查找

select field1,field2 from table
group by field1,field2  having COUNT(*)>1

删除重复值,只保留ID最小的那条


delete table
where field1 in (select field1 from VersionProductRelation a
group by field1,field2 having COUNT(*)>1)

and field2 in (select field2 from VersionProductRelation a
group by field1,field2 having COUNT(*)>1)

and ID not in (
select MIN(ID) from table 
group by field1,field2 having COUNT(*)>1)

MSSQL 删除重复并只保留一条

标签:保留   mss   sql   product   having   field   ati   sel   version   

原文地址:https://www.cnblogs.com/Alex-Mercer/p/12263095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!