SQL去除重复记录(七种)

SQL去除重复记录(七种)

2023年6月27日发(作者:)

SQL去除重复记录(七种)话不多说,请看代码:if not object_id('Tempdb..#T') is null drop table #TGoCreate table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert #Tselect 1,N'A',N'A1' union allselect 2,N'A',N'A2' union allselect 3,N'A',N'A3' union allselect 4,N'B',N'B1' union allselect 5,N'B',N'B2'Go

--I、Name相同ID最⼩的记录(推荐⽤1,2,3),保留最⼩⼀条⽅法1:delete a from #T a where exists(select 1 from #T where Name= and ID<)⽅法2:delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on = and = where is null⽅法3:delete a from #T a where ID not in (select min(ID) from #T where 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= and ID<)>0⽅法6:delete a from #T a where ID<>(select top 1 ID from #T where Name= order by ID)⽅法7:delete a from #T a where ID>any(select ID from #T where Name=)select * from #T以上就是本⽂的全部内容,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,同时也希望多多⽀持!

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687817047a47700.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信