1:
select * from table1 a
where rowid <> (select min(rowid) from table1
where 主键1= a.主键1 and 主键2= a.主键2);2:
delete table1 a
where rowid <> (select min(rowid) from table1
where 主键1= a.主键1 and 主键2= a.主键2);
select * from table1 a
where rowid <> (select min(rowid) from table1
where 主键1= a.主键1 and 主键2= a.主键2);2:
delete table1 a
where rowid <> (select min(rowid) from table1
where 主键1= a.主键1 and 主键2= a.主键2);
select pk_columns,count(*) from your_table group by pk_columns having count(*)>1;将结果插入到临时表temp_table1
insert into temp_table2 select * from your_table where pk_columns in(select pk_columns from temp_table1);删除多余记录
delete * from your_table where pk_columns in(select pk_columns from temp_table2);然后再将临时表temp_table2的数据insert into到your_table中。苯方法。
music 表里主键是ID,(有3万多),其中有1000多重复的ID ,
我想把ID一样的删除怎么写啊?
是这样吗?
delete music a
where id<> (select min(id) from music
where id= a.id )
??
多谢了先!!!!!急啊!!!1
方法原理:
1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,
rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。 2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中
那些具有最大rowid的就可以了,其余全部删除。 3、以下语句用到了3项技巧:rowid、子查询、别名。 实现方法:
SQL> create table a (
2 bm char(4), --编码
3 mc varchar2(20) --名称
4 )
5 / 表已建立. SQL> insert into a values('1111','1111');
SQL> insert into a values('1112','1111');
SQL> insert into a values('1113','1111');
SQL> insert into a values('1114','1111'); SQL> insert into a select * from a; 插入4个记录. SQL> commit; 完全提交. SQL> select rowid,bm,mc from a; ROWID BM MC
------------------ ---- -------
000000D5.0000.0002 1111 1111
000000D5.0001.0002 1112 1111
000000D5.0002.0002 1113 1111
000000D5.0003.0002 1114 1111
000000D5.0004.0002 1111 1111
000000D5.0005.0002 1112 1111
000000D5.0006.0002 1113 1111
000000D5.0007.0002 1114 1111 查询到8记录. 查出重复记录
SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc); ROWID BM MC
------------------ ---- --------------------
000000D5.0000.0002 1111 1111
000000D5.0001.0002 1112 1111
000000D5.0002.0002 1113 1111
000000D5.0003.0002 1114 1111 删除重复记录
SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc); 删除4个记录. SQL> select rowid,bm,mc from a; ROWID BM MC
------------------ ---- --------------------
000000D5.0004.0002 1111 1111
000000D5.0005.0002 1112 1111
000000D5.0006.0002 1113 1111
000000D5.0007.0002 1114 1111
delete from music a1 where a1.rowid!=(select max(rowid) from music a2 where a1.id=a2.id )