如何去除重复(两个或更多字段相同)数据?

herrojay 2009-12-11 04:10:02
比如说,现在有1张表,3个字段(id,b,c),3条数据(数据a,数据b,数据c)。
且数据b的b和c都要等于数据c的b和c才能断定为重复。
如何找出这样的数据,并且删除重复的,只保留一条。
或者一张表的字段有100个。如何做?谢谢。
...全文
4246 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sosqrn975 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liusong_china 的回复:]
SQL code
--重复数据只显示一条:
select min(id) id,b,c from tb group by b,c

--删除重复数据:
delete from tb where rowid not in (select min(rowid) from tb group by b,c);
[/Quote]

应该在多个字段的情况下会有多余记录产生,我测试了下

多个字段指除了重复的2个字段外其它未查询的字段
sosqrn975 2010-12-22
  • 打赏
  • 举报
回复
<pre>

select * from tb a join(
select repeatColumnA,repeatColumnB,repeatColumnN from tb
group by repeatColumnA,repeatColumnB,repeatColumnN
having count(*)>1
)b on a.repeatColumnA=b.repeatColumnA
and a.repeatColumnB=b.repeatColumnB
and a.repeatColumnN=b.repeatColumnN;

</pre>
xtb_0129 2009-12-18
  • 打赏
  • 举报
回复
一楼的方法可以
huangdh12 2009-12-13
  • 打赏
  • 举报
回复
相似的问题csdn有很多解答过的。。
bobo415 2009-12-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 acmain_chm 的回复:]
SQL code
delete from 1张表 a where exists (select 1 from 1张表 where 数据b=a.数据 band 数据c=a.数据c and 数据a<a.数据a)
[/Quote]
多条重复的能只剩一条吗
碧水幽幽泉 2009-12-11
  • 打赏
  • 举报
回复

create table test (id number, name varchar2(40));

insert into test(id,mame) values (1,'huangbiquan');

insert into test(id,mame) values (1,'huangbiquan');

insert into test(id,mame) values (1,'huangbiquan');

insert into test(id,mame) values (2,'xiaoquan');

insert into test(id,mame) values (3,'bishui');

insert into test(id,mame) values (4,'youyouquan');

commit;

查询相同记录
1.select * from test t where t.rowid > (select min(x.rowid) from test x where t.id = x.id);
2.select * from test t where t.rowid <> (select max(x.rowid) from test x where t.id = x.id);
3.select count(*),t.id, t.name from test t group by id,name having count(*) > 1;

查询不相同的记录
1.select * from test t where t.rowid <= (select min(x.rowid) from test x where t.id = x.id);
2.select distinct t.* from test t;

删除重复记录
1.delete from test t where t.rowid > (select min(x.rowid) from test x where t.id = x.id);
2.delete from test t where t.rowid <> (select max(x.rowid) from test x where t.id = x.id);


delete from tt
where rowid in(select rd from(
select rowid rd,row_number()over(partition by 字段 order by rownum)rn
from tt)
where rn <>1)

ACMAIN_CHM 2009-12-11
  • 打赏
  • 举报
回复
delete from 1张表 a
where exists (select 1 from 1张表 where 数据b=a.数据b and 数据c=a.数据c and 数据a<a.数据a)
sky711 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 herrojay 的回复:]
引用 1 楼 liusong_china 的回复:
SQL code--重复数据只显示一条:selectmin(id) id,b,cfrom tbgroupby b,c--删除重复数据:deletefrom tbwhere rowidnotin (selectmin(rowid)from tbgroupby b,c);


能解释下么,数据库我还很生疏。谢谢。
[/Quote]

oracle 中rowid(伪列) 用来唯一标示一行记录
上面的语句按 b, c列分组,并找出每组最小的rowid, 然后其它行的数据删除。
herrojay 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liusong_china 的回复:]
SQL code--重复数据只显示一条:selectmin(id) id,b,cfrom tbgroupby b,c--删除重复数据:deletefrom tbwhere rowidnotin (selectmin(rowid)from tbgroupby b,c);
[/Quote]

能解释下么,数据库我还很生疏。谢谢。
liusong_china 2009-12-11
  • 打赏
  • 举报
回复
--重复数据只显示一条:
select min(id) id,b,c from tb group by b,c

--删除重复数据:
delete from tb where rowid not in (select min(rowid) from tb group by b,c);

3,490

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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