利用rowid高效删除重复数据SQL如下,语句是怎么执行的?

删除数据改成了查询,方便实验。执行原理请详述!
select rowid,e.*
from scott.emp e
where rowid >
(select min(x.rowid) from scott.emp x where x.deptno = e.deptno);
select rowid,e.*
from scott.emp e;
结果是14条数据;

select rowid,e.*
from scott.emp e
where rowid >
(select min(x.rowid) from scott.emp x where x.deptno = e.deptno);
结果是11条数据;

select rowid,e.*
from scott.emp e
where rowid > 'AAAMfPAAEAAAAAgAAA';
结果是13条数据;

select min(x.rowid) from scott.emp x, scott.emp e where x.deptno = e.deptno;
结果是'AAAMfPAAEAAAAAgAAA';

是最后一个查询有问题吗?

rowid就是唯一标志记录物理位置的一个id
最後一句 select min(x.rowid) from scott.emp x where x.deptno = e.deptno 就是关联原表查询出每个deptno 最小的rowid,然後将这个结果带入语句 ,这样查询出的就是大於每个deptno 最小的rowid的所有数据,用於删除时就会将重复的删除,只留下最小rowid对应的deptno 信息追问

select rowid,e.*
from scott.emp e
where rowid >
(select min(x.rowid) from scott.emp x where x.deptno = e.deptno);
结果是11条数据;

select rowid,e.*
from scott.emp e
where rowid > 'AAAMfPAAEAAAAAgAAA';
结果是13条数据;

select rowid,e.*
from scott.emp e;
结果是14条数据;

追答

最後一个应该是表的所有数据数量,第二个语句是'>'所以不包含最小的一条信息,你用'>='应该就是14条信息,第一个语句我没有你的数据没法判断,要看看你执行其中的select min(x.rowid) from scott.emp x where x.deptno = e.deptno语句得出的结果是什麽

追问

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-11-17
这个是关联子查询,对于关联子查询来讲, 是自外到内的, 先执行外查询,
例如上面的例子, 对于主表a来间, 对于每1个deptno, 都要执行一次子查询.注意,
关联子查询还有1个限制, 就是子查询不能返回多于1行的数据.
区别于非关联子查询,的地方就是,子查询能不能单独执行,
他这个子查询本来不能执行,你改过了,你查出13条数据的查询并不是原sql的执行逻辑。
有用请点赞!!!!
第2个回答  2012-10-09
不孬
相似回答