Heim >Datenbank >MySQL-Tutorial >有一个表里面有某个字段出现重复记录,怎样保留重复记录中的第一_MySQL

有一个表里面有某个字段出现重复记录,怎样保留重复记录中的第一_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:55:16996Durchsuche

有一个表里面有某个字段出现重复记录,怎样保留重复记录中的第一条记录

select   *,identity(int,1,1)   ID   into   #t1   from   table1  
  delete   a  
  from   #t1   a  
  where   ID   >   (select   min(ID)   from   #t1   where   field1=a.field1   and  
    field2=a.field2   and   ...   fieldn=a.fieldn)  
   
  trancate   table   table1  
  alter   table   #t1   drop   column   ID  
  insert   into   table1   select   *   from   #t1

也许可以这样,简便些  
  select   distinct   *   into   #t1   from   table1  
  trancate   table   table1  
  insert   into   table1   select   *   from   #t1

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn