Oracle 重复数据查询以及删除

举报
tea_year 发表于 2021/12/29 23:53:41 2021/12/29
【摘要】 视频课:https://edu.csdn.net/course/play/7940 Create table test (id number(2),  names varchar2(20));  insert into test values(1,'张三'); insert into test valu...

视频课:https://edu.csdn.net/course/play/7940

Create table test

(id number(2),


 names varchar2(20));


 insert into test values(1,'张三');
 insert into test values(2,'李四');
 insert into test values(3,'马七');

 select * from test;


 --查找重复数据
select * from test a where (a.id,a.names) in

 (select id,names from test group by id,names having count(*) > 1)


 --删除重复数据,只留rowid最小的那行;
 delete from test a where (a.id,a.names) in

 (select id,names from test group by id,names having count(*) > 1)


  and rowid not in (select min(rowid) from test group by id,names having count(*)>1)
  --查找重复数据,不含rowid最小的行
  select * from test a where (a.id,a.names) in
  (select id,names from test group by id,names having count(*) > 1)
  and rowid not in (select min(rowid) from test group by id,names having count(*)>1)


---1.以上是重复数据根据多列来判断


以下是重复数据根据单列来判断

1、首先,查找表中多余的重复记录,重复记录是根据单个字段(id)来判断

 

select * from test where id in(select id from test group by having count(id) >1)

 

2、删除表中多余的重复记录,重复记录是根据单个字段(id)来判断,只留有rowid最小的记录

 

delete from test where (id) in (select id from test group by id having count(id) >1) and rowid not in (select min(rowid) from test group by id having count(*)>1)



文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/58661838

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。