用SQL语句删除重复记录

刚刚想整理下删除重复记录的各种解决方案,布列如下:

 

问题:如何把具有相同字段的记录删除,只留下一条。例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。

 

方法一:

  1、将重复的记录记入temp1表:

    select *  into temp1 from (

                       select * from test t where t.name in (

                          select name,count(id) as counts from test tst group by tst.name having count(id)>1

                       )

    );

 

  2、将不重复的记录插入temp2表:

     

    select *  into temp2 from (

                       select * from test t where t.name in (

                          select name,count(id) as counts from test tst group by tst.name having count(id)=1

                       )

    );

 

   3、删除原来的表: delete from test;

   4、将temp2表的记录插入原来的表:insert into test select * from temp2;

   5、将temp1表的记录插入原来的表:insert into test select distinct name from temp1;

   6、删除临时表:drop temp1; drop temp2;

 

方法二:

       declare @max integer,@id integer;
   
      declare cur_rows cursor local for 
      select id,count(*) from 表名 group by id having count(*) > 1;
      open cur_rows
      fetch cur_rows into @id,@max
      while @@fetch_status=0;
      begin
        select @max = @max -1 from dual;
        set rowcount @max;
        delete from 表名 where id = @id;
        fetch cur_rows into @id,@max;
      end;
      close cur_rows;
      set rowcount 0;

 

 

注:set rowcount @max - 1表示当前缓冲区只容纳@max-1条记录,如果有十条重复的,就刪除10条,一定会留一条的。也可以写成delete from 表名。

 

 

 

另外一种情况---如何按字段删除重复记录?  
   
  一张表里面以两个字段为唯一字段,当几条记录的这两个字段完全相同时,需要删除重复项,如下表  
  a   b   c   d  
  1   2   3   4  
  1   5   3   5  
  1   2   7   9  
  以a、b为唯一字段,第一条和第三条的a、b完全相同,所以,需要删除第一条记录1   2   3   4   或者第三条记录1   2   7   9  
  即如下结果:  
  a   b   c   d  
  1   2   3   4  
  1   5   3   5  
  或  
  a   b   c   d  
  1   5   3   5  
  1   2   7   9   
    
方法如下:
     

CREATE   TABLE   Tb1(id   int,   [a]   varchar(255),   [b]   varchar(255),   [c]   varchar(255),   [d]   varchar(255))   
  INSERT   Tb1(id,   [a],   [b],   [c],   [d])   
                        SELECT   1,   '1','2','3','4'   
  UNION   ALL     SELECT   2,   '1','5','3','5'   
  UNION   ALL     SELECT   3,   '1','2','7','9'   
  UNION   ALL     SELECT   4,   '1','4','7','6'   
    
  delete   Tb1   where   [id]   not   in   (select   max([id])   from   Tb1   group   by   a,b   )   
  select   *   from   tb1   
      
  drop   table   tb1   
    
  如果要同时删除第一和第三行   
  即如下结果:   
  a   b   c   d   
  1   5   3   5   
    
  语句如下:   
    
  delete   m   from   tb   t   
  inner   join   
  (   
  select   a   ,b   
  from   tb   
  group   by   a   ,   b   
  having   count(*)>1   
  )n   
  on   m.a   =   n.a   and   m.b   =   n.b     
  或   
  delete   *   from   tb   as   m,   
  (   
  select   a   ,b   
  from   tb   
  group   by   a   ,   b   
  having   count(*)>1   
  )n   
  where   m.a   =   n.a   and   m.b   =   n.b

 

第三种情况-----在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的
 

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断   
  select   *   from   people   
  where   peopleId   in   (select     peopleId     from     people     group     by     peopleId     having     count(peopleId)   >   1)   
    
  2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录   
  delete   from   people     
  where   peopleId     in   (select     peopleId     from   people     group     by     peopleId       having     count(peopleId)   >   1)   
  and   rowid   not   in   (select   min(rowid)   from     people     group   by   peopleId     having   count(peopleId   )>1)   
    
  3、查找表中多余的重复记录(多个字段)     
  select   *   from   vitae   a   
  where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq     having   count(*)   >   1)   
    
  4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录   
  delete   from   vitae   a   
  where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)   
  and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)>1)   
    
  5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录   
  select   *   from   vitae   a   
  where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)   
  and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)>1)   

 

   今天暂且整理到此,后续接着整理。2009-8-31 16:58

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值