刪除重複的行

問題:

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  

 

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