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);
delete from mystudent where stuno in(select distinct stuno from mystudent);
数据全部都会被删掉
分析
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 (...)