首頁 >資料庫 >Oracle >oracle怎麼查詢重複的數據

oracle怎麼查詢重複的數據

WBOY
WBOY原創
2022-02-28 11:13:5239591瀏覽

在oracle中,可以利用count()函數來配合select查詢語句來查詢重複的數據,語法為「select userCode from user group by userCode having count(userCode)>1」。

oracle怎麼查詢重複的數據

本教學操作環境:Windows10系統、Oracle 11g版、Dell G3電腦。

oracle怎麼查詢重複的資料

1、查找表中多餘的重複記錄,重複記錄是根據單一欄位(userCode)來判斷

select 
    * 
from 
    user
where 
    userCode
in 
    (select  userCode  from  user group by  userCode having count (userCode) > 1)

2、刪除表中多餘的重複記錄,重複記錄是根據單一欄位(userCode)來判斷,只留有rowid最小的記錄

delete from 
    user 
where 
    userCode 
in 
    (select userCode from user group by  userCode having count (peopleId) > 1)
and rowid not in 
    (select min(rowid) from   user group by userCode having count(userCode)>1)

3、查找表中多餘的重複記錄(多個欄位)

select 
    * 
from 
    user a
where 
    (a.userCode,a.userName) 
in  
    (select userCode,userName from user group by userCode,userName having count(*) > 1)

4、刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄

delete from 
    user a
where
    (a.userCode,a.userName) 
in   
    (select userCode,userName from user group by userCode,userName having count(*) > 1)
and rowid not in 
    (select min(rowid) from user group by userCode,userName having count(*)>1)

5、查找表中多餘的重複記錄(多個欄位),不包含rowid最小的記錄

select 
    * 
from 
    user a
where 
    (a.userCode,a.userName)  
in   
    (select userCode,userName from user group by userCode,userName having count(*) > 1)
and rowid not in 
    (select min(rowid) from user group by userCode,userName having count(*)>1)

推薦教學:《Oracle影片教學

以上是oracle怎麼查詢重複的數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn