请教一个问题,在一个表里面id  a  b  c  d 
1   1  0  0  0
2   1  1  0  0
3   1  1  1  1
4   2  1  0  0
5   1  1  1  0我需要删除 如果有一个列 它的a值和这个表中的任意一个列a值相同, b值和这个列的d值相同,但同时相等的值不能是0,就需要删除这个列  在这个例子里面,我需要删除的只有唯一的 id 是 2 的这一列,因为它符合满足 id 是3 的这一行以上条件,请问如何实现呢?提前感谢

解决方案 »

  1.   


    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    create table [test]([id] int,[a] int,[b] int,[c] int,[d] int)
    insert [test]
    select 1,1,0,0,0 union all
    select 2,1,1,0,0 union all
    select 3,1,1,1,1 union all
    select 4,2,1,0,0 union all
    select 5,1,1,1,0
    delete from test
    where exists(select 1 from test m 
    where m.id=test.id+1 and test.a=m.a and test.b=m.d and m.d<>0)select * from test/*
    id a b c d
    -------------------------------------------------------
    1 1 0 0 0
    3 1 1 1 1
    4 2 1 0 0
    5 1 1 1 0
    */
      

  2.   


    --try
    delete t from t t1
    where t.id<>t1.id and t.a=t1.a and t.b=t1.d and t1.d<>0
      

  3.   

     select a.* from tb1 a  join tb1 b on a.a=b.a and a.b =b.d and a.b<>0 and a.id<>b.id
      

  4.   

    为什么id=5的不删除?
    select a.* from tb1 a join tb1 b 
    ona.a=b.a and a.b =b.d and a.b<>0 and a.id<>b.id2 1 1 0 0
    5 1 1 1 0
      

  5.   

    如何删除呢? select貌似不能直接替换成delete
      

  6.   


    1楼的你看了?难道不是你描述的要求??--> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    create table [test]([id] int,[a] int,[b] int,[c] int,[d] int)
    insert [test]
    select 1,1,0,0,0 union all
    select 2,1,1,0,0 union all
    select 3,1,1,1,1 union all
    select 4,2,1,0,0 union all
    select 5,1,1,1,0
    delete from test
    where exists(select 1 from test m 
    where m.id=test.id+1 and test.a=m.a and test.b=m.d and m.d<>0)select * from test/*
    id    a    b    c    d
    -------------------------------------------------------
    1    1    0    0    0
    3    1    1    1    1
    4    2    1    0    0
    5    1    1    1    0
    */
      

  7.   

    如果你的删除条件不是相邻的两行作比较就这样:
    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    create table [test]([id] int,[a] int,[b] int,[c] int,[d] int)
    insert [test]
    select 1,1,0,0,0 union all
    select 2,1,1,0,0 union all
    select 3,1,1,1,1 union all
    select 4,2,1,0,0 union all
    select 5,1,1,1,0
    delete from test
    where exists(select 1 from test m 
    where m.id<>test.id and test.a=m.a and test.b=m.d and m.d<>0)
      

  8.   

      呵呵 finally...  OK 了  非常感激!