sql-删除重复数据-oracle
作者:互联网
删除重复数据
准备重复数据
create table mystudent
(
stuno number ,
stuname varchar2(20),
stuage number
);
insert into mystudent values(1,'zs',23);
insert into mystudent values(1,'zs',23);
insert into mystudent values(2,'ls',24);
insert into mystudent values(2,'ls',24);
insert into mystudent values(3,'ww',55);
尝试1:distinct
delete from mystudent where stuno in(select distinct stuno from mystudent);
数据全部都会被删掉
尝试2:伪列(rowid)
分析
select rowid, s.* from mystudent s;
rowid的组成 AAAST+ AAE AAAAJV AAA:
数据对象号(6位):AAAST+
相关数据文件号(3位):AAE
数据块号(6位):AAAAJV
数据块中行号(3位):AAA
-
将重复数据分为一组:
select min(rowid) from mystudent group by stuno,stuname,stuage ;
-
发现数据插入的越晚,rowid越大;数据插入越早,rowid越小。
最终SQL:
delete from mystudent where rowid not in(select min(rowid) from mystudent group by stuno,stuname,stuage);
将相同数据分为一组-->在每组中 找到最小的rowid,即min(rowid) -->删除不是最小的rowid (只保留最初插入的数据) ,即delete from ...not in (...)
标签:insert,stuno,删除,into,values,rowid,sql,oracle,mystudent 来源: https://www.cnblogs.com/jiyuchen1/p/16398797.html