国产探花免费观看_亚洲丰满少妇自慰呻吟_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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙门县| 和硕县| 沈丘县| 泾源县| 瓦房店市| 乾安县| 泸定县| 洱源县| 米林县| 杨浦区| 青铜峡市| 汉源县| 苗栗县| 海淀区| 松江区| 轮台县| 丹棱县| 安龙县| 石景山区| 雷波县| 枣庄市| 长垣县| 莱芜市| 北京市| 高密市| 同仁县| 长岛县| 南部县| 区。| 新丰县| 科尔| 岳普湖县| 肇东市| 常州市| 化德县| 温宿县| 赞皇县| 寿光市| 会昌县| 钟山县| 哈巴河县|