当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

 ·这种邮件服务器日志是否正常    »显示摘要«
    摘要: tue, 16 sep 2003 20:55:15 smtp登录. ip: 211.159.67.157 , port 4726 tue, 16 sep 2003 20:55:18 [211.159.67.157] 关闭连接. tue, 16 sep 2003 20:55:19 smtp登录. ip: 211.159.67.157 , port 4953 tue, 16 sep 2003 ......
    摘要: rt ......


怎样把已经有数据的表,去掉某个字段的值相同的记录呢

假设:  
  table   business  
  字段     name  
   
   
  怎样把已经有数据的表business,去掉name字段的值相同的记录呢?

NO.1   作者: txlicenhe

蚂蚁的:去除重复值  
  如果有ID字段,就是具有唯一性的字段  
   
  delect   table   where   id   not   in   (  
   
      select   max(id)   from   table   group   by   col1,col2,col3...  
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
   
   
  2,如果是判断所有字段也可以这样  
      select   *   into   #aa   from   table   group   by   id1,id2,....  
      delete   table    
      insert   into   table    
      select   *   from   #aa  
   
   
   
  3,没有ID的情况  
   
  select   identity(int,1,1)   as   id,*   into   #temp   from   tabel  
  delect   #   where   id   not   in   (  
      select   max(id)   from   #   group   by   col1,col2,col3...)  
  delect   table  
  inset   into   table(...)  
        select   .....   from   #temp  
   
   
  col1+,+col2+,...col5   联合主键  
   
   
  select   *   from     table   where   col1+,+col2+,...col5   in   (  
   
      select   max(col1+,+col2+,...col5)   from   table    
  where   having   count(*)>1  
  group   by   col1,col2,col3,col4    
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
  2,  
  select   identity(int,1,1)   as   id,*   into   #temp   from   tabel  
  select   *   from     #temp   where   id   in   (  
      select   max(id)   from   #emp   where   having   count(*)>1   group   by   col1,col2,col3...)  
   
 

NO.2   作者: mjhnet

你的表需要有一个唯一性字段才行。

NO.3   作者: letsflytogether

delete   from   tablename   a   where   a.name   in   (select   name   from   tablename   b   where   a.name<>b.name)

NO.4   作者: yujohny

有主键就好办了,设你id为主键  
  delete   from   business   a    
  where   id   not   in   (select   id   from   business   b   where   id   <>a.id   )

NO.5   作者: mjhnet

如果有ID字段,就是具有唯一性的字段  
   
  delect   table   where   id   not   in   (  
   
      select   max(id)   from   table   group   by   col1,col2,col3...  
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
  我的看法是:这种方法只能解决name只重复一次的情况,要不就是多次运行,直到没有记录重复为止。  
 

NO.6   作者: pire

create   table   #tt(name   char(50))  
   
  insert   into   #   tt   (select   distinct   name   from   business)  
  delete   from   business  
  insert   into   business   (select   *   from   #tt)  
   
  drop   table   #tt


    摘要: 系统:win2000+sp4 ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE