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

来源:这里教程网 时间:2026-03-03 16:17:00 作者:

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

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

select t.*from test1108 twhere t.tid in (select tidfrom test1108group by tidhaving count(tid) > 1);

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

delete from test1108 twhere t.tid in (select tidfrom test1108group by tidhaving count(tid) > 1)and rowid not in (select min(rowid)from test1108group by tidhaving count(tid) >1);

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

select * from test112101 twhere (t.tid,t.tname) in (select tid,tnamefrom test112101 t1group by t1.tid,t1.tname having count(*) >1);

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

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

相关推荐