删除表中重复的记录

 情形一:所有的字段完全重复

         方法一:

                       步骤1:查找出完全重复的记录 select ID from tablename group by ID having count(ID) > 1

                       步骤2:删除表中的ID在以上结果集中 并且 rowid不是最小的那些记录

  delete from ID
where ID in (select ID from people group by ID having count(ID ) > 1)
and ID not in (select min(rowid) from tablename group by ID having count(ID )> 1)

 

       方法二:用distinct过滤掉重复的记录,将结果集保存到临时表里,

                        然后清空表内的内容,最后将临时表的记录写入表

 

 

情形二:部分字段重复

        步骤1:查找出部分字段重复的记录 select username,usertel from tablename group by username,usertel having count(*) > 1

        步骤2:

delete from tablename
where ( username,usertel ) in (select  username,usertel from tablename group by username,usertel having count(*) > 1)
and rowid not in (select min(rowid) from tablename group by  username,usertel having count(*)> 1)

 

 

--以下是删除重复数据的3种方案
--
3种方案都是最优化的
--
object_name, object_id. 这2个字段用来判断重复

--1.
delete
 
from x
where rowid in
        (
       
select rd
         
from (
               
select rowid rd
                    ,row_number()
over(partition by object_name, object_id order by rowid) rn
                 
from x
               ) x
        
where rn > 1
       )

--2.
delete
 
from x
where rowid not in (
            
select max(rowid)
              
from x
             
group by object_name, object_id
     )     
    
--3.    
create table tmp_x
AS
   
select x1.(字段列表..略)
     
from
              (
               
select x.*, row_number() over(partition by object_name, object_id order by rowid) rn
                 
from x   
              ) x1
    
where rn = 1;
    
truncate table x;

insert into x
   
select *
     
from tmp_x;    

drop table tmp_x;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值