SQL 中删除重复的记录

  select   distinct   *   into   temp   from   yourtable  
  truncate   table   yourtable  
  insert   into   yourtable   select   *   from   temp  
  drop   table   temp

 

 

 

 

 

 

 

 谢谢各位。那要删除字段1重复的记录,比如:  
  字段1     字段2       字段3  
  11             232           32    
  11             323           3    
  11               56           5  
  现只要一条记录  
  11             232           32    
  要怎样做呢?

 

 

加一个字段id  
  alter   table   tablename   add   id   int   identity(1,1)not   null  
  go  
  select   *   from   tablename   where   id   in(select   min(id)   from   tablename   group   by   字段1)  
   
  或临时表  
  select   id=identity(int,1,1),*   into   temp   from   tablename  
  select   *   from   temp   where   id   in(select   min(id)   from   temp   group   by   字段1)  
  drop   table   temp  

 

 

 

 

 

 

 

 

 

 

這裡有兩種情況:  
  (1.)   對完全重復的記錄(即所有子段均重復)  
            select   distinct   *  
            into   #temp  
            from   tablename  
            GO  
            truncate   table   tablename  
            GO  
            select   *  
            into   tablename  
            from   #temp  
            GO  
            drop   table   #temp  
   
  (2.)對部分關鍵字重復的記錄,如重復的字段為   name,address   ;要求得到這兩個字段唯一的  
          結果集.  
          select   identity(int,1,1)   as   id,*  
          into   #temp  
          from   tablename  
          GO  
          select   min(id)   as   id  
          into   #tempyh  
          from   #temp  
          group   by   name,address  
          GO  
          select   *  
          from   #temp  
          where   id   IN   (   select   id   from   #tempyh   )  
          GO   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值