国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQL去除重復記錄(七種)

2024-08-31 01:04:39
字體:
來源:轉載
供稿:網友

話不多說,請看代碼:

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=a.Name and ID<a.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為唯一時可用):

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 ID<a.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

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到MSSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青龙| 西乡县| 屏东县| 博兴县| 陕西省| 彰武县| 河源市| 江华| 张掖市| 原阳县| 金昌市| 资溪县| 湖南省| 万安县| 镇雄县| 获嘉县| 政和县| 北流市| 吴旗县| 鄱阳县| 徐州市| 黔南| 万全县| 湟源县| 黄龙县| 揭西县| 长兴县| 昌邑市| 池州市| 宝清县| 天台县| 龙里县| 于都县| 尉氏县| 留坝县| 师宗县| 梓潼县| 班戈县| 宁安市| 鹤庆县| 兴义市|