Oracle中怎么去掉重复的行?

yezilove7374521 2009-06-19 04:34:49
Oracle中怎么去掉重复的行?
...全文
2957 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
woniu4500 2011-06-24
  • 打赏
  • 举报
回复
用distinct
hhuxyb 2009-06-26
  • 打赏
  • 举报
回复

delete from users where rowid in(select rowid from
(select id,name,rowid, row_number() over(partition by id ,name order by id,name) rid
from users) a
where rid>=2
)
gerywolf 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 heaven33 的回复:]
是删除还是显示的时候去除呢?
删除是:
delete from table1 t1 where rownum <(select max(rowunm) from table1 t2 where t1.id = t2.id)
[/Quote]

显示就是distinct咯~
maitianhust 2009-06-25
  • 打赏
  • 举报
回复
这个问题很经典,有多种做法。
可以先建立一个临时表,找出表中所有不重复的数据插入新表,然后删除原表,再将临时表中数据插入到原表。
或者用一条SQL语句执行,

delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
正解,不过要求ID是主键,或者该字段具有唯一性约束,否则会多删除数据。
雪狼__ 2009-06-25
  • 打赏
  • 举报
回复
根据rowid不同
distinct查询时用
一嘴绿茶 2009-06-25
  • 打赏
  • 举报
回复
delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
正解,不过要求ID是主键,或者该字段具有唯一性约束,否则会多删除数据。
Ying2008 2009-06-25
  • 打赏
  • 举报
回复
DISTINCT
liurui1985 2009-06-24
  • 打赏
  • 举报
回复
楼上的几位兄弟都是正解,不过感觉效率上。。。。
435002 2009-06-24
  • 打赏
  • 举报
回复
百度一下row_number()函数的用法就知了!
csuxp2008 2009-06-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 you_tube 的回复:]
SQL code;with args as
(
select id,name,row_number() over(parition by name order by name) Rownumber
from Tab
)
delete from Args
where Rownumber >=2
[/Quote]

12楼的可否解释一下,不太懂
csuxp2008 2009-06-23
  • 打赏
  • 举报
回复
贴SQL代码的时候少了一点,我把select * from emp;补上


SQL> select * from emp;

ID NAME
---------- --------------------
1001 张三
1002 王五

csuxp2008 2009-06-23
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 anxing 的回复:]
delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
[/Quote]

楼上的delete语句不够完善,如果id相同,其余字段有全部相同,也有不同的话,那么最后肯定删多了。



ID NAME
---------- --------------------
1001 张三
1001 张三
1002 李四
1002 李四
1002 王五

SQL> delete from emp a where rowid < (select max(rowid) from emp b where a.id=b.id);

已删除3行。

SQL> select * from emp;

ID NAME
---------- --------------------
1001 张三
anxing 2009-06-22
  • 打赏
  • 举报
回复
delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
cab1225 2009-06-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 heaven33 的回复:]
应该是:
delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
[/Quote]
这个是队的!!!
tianyazlf 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 heaven33 的回复:]
应该是:
delete from table1 t1 where rowid <(select max(rowid) from table1 t2 where t1.id = t2.id)
[/Quote]

7楼的方法删除是所有id重复的行,如果表中id相同,其余栏位不同的资料会被删的只剩一条记录;

这个应该看楼主的需求了,是要删除哪些栏位重复的资料,在后面继续添加条件;

不过如果表的数据很多,效率可能很低!
inthirties 2009-06-20
  • 打赏
  • 举报
回复
distinct
you_tube 2009-06-20
  • 打赏
  • 举报
回复
;with args as
(
select id,name,row_number() over(parition by name order by name) Rownumber
from Tab
)
delete from Args
where Rownumber >=2
lixiaolong3399 2009-06-20
  • 打赏
  • 举报
回复
既然大家都说了,我就没什么好说的, 祝大家进步进步。
yezilove7374521 2009-06-20
  • 打赏
  • 举报
回复
谢谢!
pathuang68 2009-06-19
  • 打赏
  • 举报
回复
加载更多回复(8)

17,086

社区成员

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

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