id    name   age
1     jack   20
2     rose   30
3     jack   20
4     book   100
5     bady   30
6     rose   30如上面这个 ,我需要删除其他重复的字段!!!! 根据ID删除 大虾们帮帮忙啊。

解决方案 »

  1.   

    delete from t where id<>(select min(id) from t group by name,age)
      

  2.   

    改下:
    delete from t where id not in (select min(id) from t group by name,age)
      

  3.   

    delete t
    from tb t
    where exists(select 1 
                 from tb
                 where name=t.name and age=t.age and id>t.id)
      

  4.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([id] int,[name] nvarchar(4),[age] int)
    Insert tb
    select 1,N'jack',20 union all
    select 2,N'rose',30 union all
    select 3,N'jack',20 union all
    select 4,N'book',100 union all
    select 5,N'bady',30 union all
    select 6,N'rose',30
    Go
    delete t
    from tb t
    where exists(select 1 
                 from tb
                 where name=t.name and age=t.age and id>t.id)
    select *
    from tb
    /*
    id          name age
    ----------- ---- -----------
    3           jack 20
    4           book 100
    5           bady 30
    6           rose 30(4 個資料列受到影響)
    */
      

  5.   

    delete t from tb t
    where id>(select min(id) 
    from tb 
    where t.name=name and age=t.age )
      

  6.   

    if not object_id('tb') is null
        drop table tb
    Go
    Create table tb([id] int,[name] nvarchar(4),[age] int)
    Insert tb
    select 1,N'jack',20 union all
    select 2,N'rose',30 union all
    select 3,N'jack',20 union all
    select 4,N'book',100 union all
    select 5,N'bady',30 union all
    select 6,N'rose',30
    Godelete b from tb b   
    where exists
    (select * from tb where [name]=b.[name] and [age]=b.[age] and [id]<b.[id])select * from tbid          name age
    ----------- ---- -----------
    1           jack 20
    2           rose 30
    4           book 100
    5           bady 30(4 行受影响)
      

  7.   

    delete t from tb t
    where id>(select min(id) 
                from tb 
                where t.name=name and age=t.age )UP 这个没问题的!
      

  8.   


    delete t from tb t
    where id><(select min(id)from tb  
      where t.name=name and age=t.age )
      

  9.   

    Create table tb([id] int,[name] varchar(4),[age] int)
    Insert tb
    select 1,'jack',20 union all
    select 2,'rose',30 union all
    select 3,'jack',20 union all
    select 4,'book',100 union all
    select 5,'bady',30 union all
    select 6,'rose',30select * from tbdelete from tb where id not in(select max(id) from tb group by name)
      

  10.   

    上亿条也是这样。多字段重复比较并删除,建议用exists操作,看5L的。
    如果用not in 会让人哭的,原因不说了。
      

  11.   


    DELETE tb FROM tb t WHERE EXISTS (SELECT 1 FROM tb WHERE NAME =t.name AND age=t.age AND id>t.id)
      

  12.   

    delete from tb t where exists (select 1 from tb where name=t.name and age=t.age and id<>t.id);
      

  13.   

    delete from 表 a where exists(select * from 表 where name=a.name and age=a.age and id<a.id)
      

  14.   

    参照方法
    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html
      

  15.   

    drop table #test
    go
    create table #test
    (
    id int,
    name varchar(100),
     age int
    )
    goinsert into #test values(1 ,'jack', 20)
    insert into #test values(2 ,'rose', 30)
    insert into #test values(3 ,'jack', 20)
    insert into #test values(4 ,'book', 100)
    insert into #test values(5 ,'bady', 30)
    insert into #test values(6 ,'rose', 30)
    goselect * from #test
    godelete from #test
    where id not in 
    (
    select max(id) from #test
    group by name, age
    )
    go 
    select * from #test
    go3 jack 20
    4 book 100
    5 bady 30
    6 rose 30
      

  16.   


    if object_id('tb') is not null drop table tb
    go
    create table tb
    (
        id int,
    [name] varchar(25),
        age int
    )
    insert tb
    select 1,'jack',20 union all
    select 2,'rose',30 union all
    select 3,'jack',20 union all
    select 4,'book',100 union all
    select 5,'bady',30 union all
    select 6,'rose',30 
    go
    begin tran
    delete  t from
    [tb] t
    where id <> (select min(id) from tb where t.[name]=[name] and t.age=age)select * from tb
    rollback tran
      

  17.   

    delete from t where id < (select max(id) from t group by name,age)