sqlserver清除完全重复的数据只保留重复数据中的第一条
--创建测试表 CREATE TABLE [dbo].[testtab]( [id] [nchar](10) NULL, [name] [nchar](10) NULL ) ; --向测试表插入测试数据 insert into testtab values('1','1'); insert into testtab values('1','1'); insert into testtab values('2','2'); insert into testtab values('2','2'); insert into testtab values('3','3'); insert into testtab values('3','3'); --创建临时表并向临时表中插入测试表testtab中数据以及添加自增id:autoID select identity(int,1,1) as autoID, * into #Tmp from testtab --根据autoID删除临时表#tmp中的重复数据,只保留每组重复数据中的第一条 delete #Tmp where autoID in(select max(autoID) from #Tmp group by id); --清除testtab表中的所有数据 delete testtab; --向testtab表中插入#Tmp表中被处理过的数据 insert into testtab select id,name from #Tmp; --删除临时表#Tmp drop table #Tmp;
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30