加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL去除重复记录(七种)

发布时间:2020-12-12 09:56:43 所属栏目:MsSql教程 来源:网络整理
导读:话不多说,请看代码: --I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条 方法1: delete a from #T a where exists(select 1 from #T where Name=a.Name and ID 方法2: 方法3: delete a from #T a where ID not in (select min(ID) from #T where Name=a

话不多说,请看代码:

--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条

方法1:

delete a from #T a where exists(select 1 from #T where Name=a.Name and ID

方法2:

方法3:

delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)

方法4(注:ID为唯一时可用):

delete a from #T a where ID not in(select min(ID)from #T group by Name)

方法5:

delete a from #T a where (select count(1) from #T where Name=a.Name and ID0

方法6:

delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)

方法7:

any(select ID from #T where Name=a.Name) select * from #T

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程之家!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读