删除重复行有两种方法:
数据准备
建表语句
create table a(a varchar2(10),b varchar2(20));
插入数据
insert into a values('11','22');
insert into a values('11','22');
insert into a values('11','22');
insert into a values('aa','bb');
insert into a values('aa','bb');
insert into a values('cc','dd');
commit;
克隆一张表
create table test as (select * from a);
查询(1)select * from test
1 11 22
2 11 22
3 11 22
4 aa bb
5 aa bb
6 cc dd
(2)
select distinct * from test;
1 11 22
2 cc dd
3 aa bb
1)利用中间表法:create table test_copy as (select distinct * from test);
然后删除原表 drop table test;
create table test as (select * from test_copy);
然后就完成了。
2)利用rowid法
sql语句如下:
delete from test t where rowid not in(
select max(rowid) from test p where t.a=p.a and t.b=p.b);
commit;
本文介绍两种有效删除数据库中重复行的方法:一是通过创建中间表并使用DISTINCT关键字选择唯一记录;二是利用ROWID来标识每条记录的唯一性,通过子查询找出每个组中的最大ROWID,从而删除重复项。
2044

被折叠的 条评论
为什么被折叠?



