有两个表   
表a:    原料ID   数量
         1        2
         2        4
         4        7表b:    原料ID   采购数量
         1          22
         2          33
         3          55
         4          11
         5          77
请问如何得到表b中有的原料ID,但表a中没有该原料ID
表c:    原料ID    采购数量
          3         55
          5         77谢谢!!

解决方案 »

  1.   

    select *
    from b
    where not exists(select 1 from a where a.原料ID=b.原料ID)
      

  2.   

    select * from tb
    where 原料ID not in(select 原料ID from ta)
      

  3.   

    create table a(原料ID int,数量 int)
    create table b(原料ID int,采购数量 int)
    insert into a
     select  1, 2 union all
     select  2 ,4 union all
     select  4 ,7
    insert into b
     select  1 ,22 union all
     select  2 ,33 union all
     select   3 ,55 union all
     select  4 ,11 union all
     select  5 ,77
    GO
    SELECT * FROM B WHERE 原料ID NOT IN(select a.原料ID from a inner join b on a.原料ID=b.原料ID)3 55
    5 77
      

  4.   


    select b.* from b left join a 
    on b.原料ID=a.原料ID
    where a.原料ID is null
      

  5.   

    select * FROM B WHERE 原料ID not in(select a.原料ID from a )