日期:2014-05-17  浏览次数:20793 次

利用rowid删除重复记录?
我利用rowid删除表t中的重复记录,比如我认为a,b相同就是重复的,需要删除之后只保留一条。
一开始这样写:
delete from t where rowid not in (select rowid from (select distinct a, b from t));
结果不行。

后来写成这样:

delete from t where rowid not in (select max(rowid) from t group by a, b);
就OK了。

有没有哪位知道第一种写法为啥无效?

------解决方案--------------------
ROWID是对于一张具体表的概念,比如说你的t表
select rowid from (select distinct a, b from t)
 (select distinct a, b from t) 不是一张确实的表,Oracle是无法辨认他的ROWID的
------解决方案--------------------
delete from t m where rowid <(select max(rowid) from t n where m.a=n.a and m.b=n.b);


------解决方案--------------------
探讨
我利用rowid删除表t中的重复记录,比如我认为a,b相同就是重复的,需要删除之后只保留一条。
一开始这样写:
delete from t where rowid not in (select rowid from (select distinct a, b from t));
结果不行。

后来写成这样:

delete from t where rowid not in (sel……

------解决方案--------------------
Rowid 不能与distinct一起使用,在Oracle 11g R2中执行这样的sql,会报错提示,原因如1楼