您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)oracle中如何刪除重數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
前期準(zhǔn)備
創(chuàng)建測試表
create table salary(
staffid int,
staff varchar(15)
);
模擬重復(fù)數(shù)據(jù)
insert into salary values(1,'a');
insert into salary values(2,'s');
insert into salary values(3,'ert');
insert into salary values(4,'d');
insert into salary values(5,'b');
insert into salary values(1,'a');
insert into salary values(2,'s');
insert into salary values(3,'ert');
insert into salary values(4,'d');
insert into salary values(5,'b');
insert into salary values(1,'a');
insert into salary values(2,'s');
insert into salary values(3,'ert');
insert into salary values(4,'d');
insert into salary values(5,'b');
insert into salary values(10,'aaaa');
insert into salary values(20,'sass');
insert into salary values(30,'erwt');
insert into salary values(40,'dsd');
insert into salary values(50,'bsdf');
insert into salary values(1,'oookkk');
實驗一:模擬單個字段數(shù)據(jù)重復(fù)
select * from salary;
STAFFID STAFF
--------------- ---------------
1 oookkk
1 a
2 s
3 ert
4 d
5 b
1 a
2 s
3 ert
4 d
5 b
1 a
2 s
3 ert
4 d
5 b
10 aaaa
20 sass
30 erwt
40 dsd
50 bsdf
21 rows selected
1.查出重復(fù)數(shù)據(jù)
方法一
SELECT *
FROM salary a
WHERE ((SELECT COUNT(*)
FROM salary
WHERE staffid = a.staffid) > 1)
ORDER BY staffid
方法二
select *
from salary
where staffid in
(select staffid from salary group by staffid having count(staffid) > 1)
刪除重復(fù)數(shù)據(jù),只保留1條,其余全部刪除
方法一,通過rowid刪除
delete from salary
where staffid in (select staffid from salary group by staffid having count(staffid) > 1)
and rowid not in (select min(rowid) from salary group by staffid having count(staffid )>1)
實驗二:模擬兩個個字段數(shù)據(jù)重復(fù)
1.查詢重復(fù)記錄
方法一
select * from salary a
where (a.staffid,a.staff) in (select staffid,staff from salary group by staffid,staff having count(*) > 1)
方法二
SELECT *
FROM salary a
WHERE ((SELECT COUNT(*)
FROM salary
WHERE staffid = a.staffid and staff=a.staff) > 1)
ORDER BY staffid
結(jié)果,共15條
STAFFIDSTAFF
1 a
1 a
1 a
2 s
2 s
2 s
3 ert
3 ert
3 ert
4 d
4 d
4 d
5 b
5 b
5 b
2.刪除重復(fù)數(shù)據(jù),只保留1條,其余全部刪除
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
3.查看刪除后結(jié)果
select * from salary;
結(jié)果
STAFFIDSTAFF
1 oookkk
1 a
2 s
3 ert
4 d
5 b
10 aaaa
20 sass
30 erwt
40 dsd
50 bsdf
看完上述內(nèi)容,你們對oracle中如何刪除重數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。