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

Sql删除表中多余的重复记录

时间:2017-10-31 14:09:42      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:group   重复   记录   bsp   min   字段   uniq   char   having   

删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete from people 
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1) 
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1) 

如果 peopleId 是uniqueidentifier类型 则将

select min(peopleId) from people group by peopleName     having count(peopleName)>1

改为

select min(convert(varchar(36),peopleId)) from people group by peopleName     having count(peopleName)>1

Sql删除表中多余的重复记录

标签:group   重复   记录   bsp   min   字段   uniq   char   having   

原文地址:http://www.cnblogs.com/xuhongcai/p/7760854.html

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