話不多說(shuō),請(qǐng)看代碼:
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 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=a.Name and IDa.ID)
方法2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
方法3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
方法4(注:ID為唯一時(shí)可用):
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 IDa.ID)>0
方法6:
delete a from #T a where ID>(select top 1 ID from #T where Name=a.name order by ID)
方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
您可能感興趣的文章:- 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
- sqlserver 用戶權(quán)限管理,LINQ去除它的重復(fù)菜單項(xiàng)
- sqlserver利用存儲(chǔ)過(guò)程去除重復(fù)行的sql語(yǔ)句
- mysql SELECT語(yǔ)句去除某個(gè)字段的重復(fù)信息
- Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
- MySQL中distinct語(yǔ)句去查詢重復(fù)記錄及相關(guān)的性能討論
- SQL高級(jí)應(yīng)用之同服務(wù)器上復(fù)制表到另一數(shù)據(jù)庫(kù)中并實(shí)現(xiàn)去重復(fù)
- SQL分組排序去重復(fù)的小實(shí)例
- oracle sql 去重復(fù)記錄不用distinct如何實(shí)現(xiàn)
- SQL語(yǔ)句去掉重復(fù)記錄,獲取重復(fù)記錄