利用rowid删除重复记录?

tusijun 2012-03-23 02:42:39
我利用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了。

有没有哪位知道第一种写法为啥无效?
...全文
173 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanita 2012-03-24
  • 打赏
  • 举报
回复
Rowid 不能与distinct一起使用,在Oracle 11g R2中执行这样的sql,会报错提示,原因如1楼
lxyzxq2008 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 tusijun 的回复:]
我利用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……
[/Quote]
很简单,因为你的子查询(select distinct a,b from t)中根本就没有rowid啊?
rowid不是你想有它就有的啊,它是oracle给数据库表绑定的定向,可不是给任意的集合的。
贝鱼 2012-03-23
  • 打赏
  • 举报
回复
delete from t m where rowid <(select max(rowid) from t n where m.a=n.a and m.b=n.b);

yinan9 2012-03-23
  • 打赏
  • 举报
回复
ROWID是对于一张具体表的概念,比如说你的t表
select rowid from (select distinct a, b from t)
(select distinct a, b from t) 不是一张确实的表,Oracle是无法辨认他的ROWID的

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧