我利用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了。有没有哪位知道第一种写法为啥无效?

解决方案 »

  1.   

    ROWID是对于一张具体表的概念,比如说你的t表
    select rowid from (select distinct a, b from t)
     (select distinct a, b from t) 不是一张确实的表,Oracle是无法辨认他的ROWID的
      

  2.   

    delete from t m where rowid <(select max(rowid) from t n where m.a=n.a and m.b=n.b);
      

  3.   

    很简单,因为你的子查询(select distinct a,b from t)中根本就没有rowid啊?
    rowid不是你想有它就有的啊,它是oracle给数据库表绑定的定向,可不是给任意的集合的。
      

  4.   

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