删除表中除ID列外其他 完全相同的记录。
比如。
ID   NAME  SEX  CLASS
1    张三  0     32  <----判读与这行除ID外其他所有都相同的行删除。这里就是删除第3行, 一条语句怎么写? 
2    李四  1     34     
3    张三  0     32 
4    王五  1     12
5    张三  0     22

解决方案 »

  1.   

    delete tb from tb t where id not in (select min(id) from tb where name = t.name and sex = t.sex and class = t.class)
      

  2.   

    delete
     t 
    from
     tb t 
    where
     exists(select 1 from tb where id<t.id and name=t.name and sex=t.sex and class=t.class)
      

  3.   

    create table tb(ID int,NAME varchar(10),SEX int,CLASS int)
    insert into tb values(1 ,'张三', 0, 32)
    insert into tb values(2 ,'李四', 1, 34) 
    insert into tb values(3 ,'张三', 0, 32 )
    insert into tb values(4 ,'王五', 1, 12)
    insert into tb values(5 ,'张三', 0, 22)
    godelete tb from tb t where id not in (select min(id) from tb where name = t.name and sex = t.sex and class = t.class)select * from tbdrop table tb/*
    ID          NAME       SEX         CLASS       
    ----------- ---------- ----------- ----------- 
    1           张三         0           32
    2           李四         1           34
    4           王五         1           12
    5           张三         0           22(所影响的行数为 4 行)
    */
      

  4.   

    delete from Table1 A where ID in (select ID from Table1 where A.Name=Name and A.Sex=Sex and A.Class=Class)