a 表
id , code
1    1
2    1
b表
id code
1   a
1   b
1   1
2   e
2   c
想找出a表中的
2,  1这条记录
就是找到a表中的id和code在b表中一条记录都没有对应的记录
where 条件该如何写呢?

解决方案 »

  1.   

    select a.* from a where not exists(select 1 from b where b.id = a.id and b.code = a.code)
      

  2.   

    create table a(id int, code varchar(10))
    insert into a values(1 ,   1 )
    insert into a values(2 ,   1 )
    create table b(id int, code  varchar(10))
    insert into b values(1 , 'a') 
    insert into b values(1 , 'b') 
    insert into b values(1 , '1') 
    insert into b values(2 , 'e') 
    insert into b values(2 , 'c') 
    goselect a.* from a where not exists(select 1 from b where b.id = a.id and b.code = a.code)drop table a , b/*
    id          code       
    ----------- ---------- 
    2           1(所影响的行数为 1 行)
    */
      

  3.   

    select a.* 
    from a, b
    where a.id<>b.id and a.code<>b.code
      

  4.   

    select a.* from a where not exists(select 1 from b where b.id = a.id and b.code = a.code)问题解决了,但是这个语句怎么理解呢?还请不惜赐教.
    where 与not exists之间是等价于a.id 或者a.id and a.code?
    感觉象是做了一个oralce中的 minus操作,不是很好理解.