【Oracle_SQL】查找/删除重复的数据(单个字段和多个字段条件)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/debimeng/article/details/84324170

oracle查找/删除重复的数据(单个字段和多个字段条件)

单个字段:
--查找重复的全部数据(单个字段)
思路:
1.根据字段tid分组,数量大于1的表示tid字段有重复的数据;
2.根据1查询出来的tid数据为条件,再查询全部重复的数据。
SQL:
 

select t.* from test1108 t
where t.tid in (select tid from test1108 group by tid having count(tid) > 1);

--删除多余的重复数据(单个字段),重复的数据保留rowid最小的行
思路:
1.根据字段tid分组,数量大于1的表示有重复数据;
2.因表中每行都有一个伪列rowid,查找出重复数据中最小的rowid;
3.根据1、2的并行条件重复的数据,但保留重复数据中rowid最小的值。
SQL:
 

delete from test1108 t
where t.tid in (select tid from test1108 group by tid having count(tid) > 1)
and rowid not in (select min(rowid) from test1108 group by tid having count(tid) >1);

多个字段:
--查找重复的全部数据(多个字段)
思路:
1.根据字段tid,tname分组,数量大于1的表示字段tid,tname有重复的数据;
2.根据1查询出来的tid,tname数据为条件,再查询全部重复的数据。
SQL:
 

select * from test112101 t
where (t.tid,t.tname) in (
  select tid,tname 
  from test112101 t1 
  group by t1.tid,t1.tname having count(*) >1
);

--删除多余的重复数据(多个字段),重复的数据保留rowid最小的行
思路:
1.根据字段tid,tname分组,数量大于1的表示有重复数据;
2.因表中每行都有一个伪列rowid,查找出重复数据中最小的rowid;
3.根据1、2的并行条件重复的数据,但保留重复数据中rowid最小的值。
SQL:
 

delete from test112101 t
where (t.tid,t.tname) in (
  select tid,tname 
  from test112101 t1 
  group by t1.tid,t1.tname having count(*) >1
)
and rowid not in(
  select min(rowid) from test112101 
  group by tid,tname having count(1)> 1);

猜你喜欢

转载自blog.csdn.net/debimeng/article/details/84324170