删除重复的行

问题:

table  
  id   field1     field2  
  1     1               qq  
  2     1               qq  
  3     2               aa  
  4     2               aa  
   
  如何将重复的行删除掉,结果为  
  table  
  id   field1     field2  
  1     1               qq  
  3     2               aa

 

 

解决办法:

1.如果有id  
  delete   from   tablename   a   where   exists(select   *   from   tablename   where   a.id<id   and   a.col1=col1)   
2.如果没有id  
  alter   table   tablename   add   id   int   identity(1,1)  
  delete   from   tablename   a   where   exists(select   *   from   tablename   where   a.id<id   and   a.col1=col1)  
  alter   table   drop   cloumn   id  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章