先有表 如下
id           A          B
10001   0403a-6-13   0516a-1-1
10002   0516a-1-1    0403a-6-13  
A 和B两端相同的话 ,即为重复,需要删除10002。请各位指点一下

解决方案 »

  1.   

    有没有什么要求呢比如说是删除id为10001的记录还是删除id为10002的记录
      

  2.   

    select min(id),a,b
    from table
    group by a,b
      

  3.   

    select id,a,b
    from tab 
    where id not in(
    --选出重复的大的ID
    select max(t.id)
    from 
    (select id,a,b from tab
    union all
    select id,b as a,a as b from tab) t
    group by t.a,t.b
    having count(*)>1
    )
      

  4.   

    select f.t,min(f.id) from ( 
    select id,case when a.a>a.b then a.a||a.b else a.b||a.a end t from tab a) f
    group by f.t
    ;
      

  5.   

    delete from tab e where e.id not in  (select min(f.id) from ( 
    select id,case when a.a>a.b then a.a||a.b else a.b||a.a end t from tab a) f
    group by f.t)
    ;
      

  6.   

    delete from tb t1
    where not exists ( select 1
                         from tb t2
                        where t2.a=t1.a and t2.b=t1.b
                         group by t2.a, t2.b
                         having min(id)=t1.id );
      

  7.   

    -- 两端相同:加个or条件
    delete from tb t1
    where not exists ( select 1
                         from tb t2
                        where (t2.a=t1.a and t2.b=t1.b) or (t2.a=t1.b and t2.b=t1.a)
                         group by t2.a, t2.b
                         having min(id)=t1.id );
      

  8.   

    group by 所有字段 也可以 
      

  9.   

    -- 正确答案:
    delete from tb t1
    where exists ( select 1
                     from tb t2
                    where ((t2.a=t1.a and t2.b=t1.b) or (t2.a=t1.b and t2.b=t1.a))
                      and t2.id>t1.id );
      

  10.   

    如果id列唯一的话,保留小的id值数据:delete from test1 t1
     where t1.id > (select min(id)
                      from test1 t2
                     where t1.a = t2.a
                       and t1.b = t2.b)
       and exists (select 1
              from test1 t2
             where t1.a = t2.a
               and t1.b = t2.b)
      

  11.   

    保留ROWID最大的记录,其余全部删除delete from table a 
    where a.rowid!=(select max(rowid) from a b where (a.a=b.a and a.b=b.b) or (a.a=b.b and a.b=b.a)); 
      

  12.   

    delet tablename where id in (select (case when T001.A=T002.B and T001.B=T002.B then T002.ID end) deletID  from tableName T001 ,tableName T002 where T001.Id=T002.ID)
      

  13.   

     
    19:47:42 SQL> select * from tb;
     
     ID A          B
    --- ---------- ----------
      2 345        123
      1 123        345
      3 123        345
     
    19:47:46 SQL> delete from tb where id in
               2  (
               3  select distinct(aa.id) from tb aa,tb bb
               4  where aa.a=bb.b and bb.a=aa.b and aa.a<>bb.a
               5  and aa.id<>(
               6  select min(aa.id) from tb aa,tb bb
               7  where aa.a=bb.b and bb.a=aa.b and aa.a<>bb.a
               8  )
               9  );
     
    2 rows deleted
     
    19:55:20 SQL> select * from tb;
     
     ID A          B
    --- ---------- ----------
      1 123        345
     
    19:55:30 SQL>