溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SQL Server中怎么刪除重復(fù)行

發(fā)布時(shí)間:2021-07-27 15:19:05 來源:億速云 閱讀:215 作者:Leah 欄目:數(shù)據(jù)庫

今天就跟大家聊聊有關(guān)SQL Server中怎么刪除重復(fù)行,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.如果有ID字段,就是具有唯一性的字段復(fù)制代碼 代碼如下: delect table where id not in ( select max(id) from table group by col1,col2,col3... )

group by 子句后跟的字段就是你用來判斷重復(fù)的條件,如只有col1,那么只要col1字段內(nèi)容相同即表示記錄相同。 2. 如果是判斷所有字段也可以這樣復(fù)制代碼 代碼如下: select * into #aa from table group by id1,id2,.... delete table insert into table select * from #aa

3. 沒有ID的情況復(fù)制代碼 代碼如下: select identity(int,1,1) as id,* into #temp from tabel delect # where id not in ( select max(id) from # group by col1,col2,col3...) delect table inset into table(...) select ..... from #temp

4. col1+','+col2+','...col5 聯(lián)合主鍵復(fù)制代碼 代碼如下: select * from table where col1+','+col2+','...col5 in ( select max(col1+','+col2+','...col5) from table where having count(*)>1 group by col1,col2,col3,col4 )

group by 子句后跟的字段就是你用來判斷重復(fù)的條件,如只有col1,那么只要col1字段內(nèi)容相同即表示記錄相同。 5.復(fù)制代碼 代碼如下: select identity(int,1,1) as id,* into #temp from tabel select * from #temp where id in ( select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

6.復(fù)制代碼 代碼如下: select distinct * into #temp from tablename delete tablename go insert tablename select * from #temp Sqlclub go drop table #temp

看完上述內(nèi)容,你們對(duì)SQL Server中怎么刪除重復(fù)行有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI