数据库中有许多重复记录
如何删除多余的,只保留其中一条?

解决方案 »

  1.   

    select * from table1 a where not exists (select 1 from table1 b where b.name=a.name and a.id>b.id)
      

  2.   

    declare @a table(id int,name varchar(20))
    insert @a
    select 1,'aa'
    union all
    select 2,'bb'
    union all
    select 3,'aa'
    delete a from @a a where  exists(select 1 from @a where name=a.name and a.id>id )select * from @a
    /*(所影响的行数为 3 行)
    (所影响的行数为 1 行)id          name                 
    ----------- -------------------- 
    1           aa
    2           bb(所影响的行数为 2 行)
    */