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

解决方案 »

  1.   

    好象不对,我想查数据库aa,和数据库bb中的两个相同的表,其中aa中的a表和bb中的b表相同,但是记录不同,有相同的id;我想查询出a中有的记录而b中没有的记录,其中a包含b
      

  2.   

    select * from  bb.dbo.b  where bb.dbo.b.id not in (select aa.dbo.a.id from aa.dbo.a)
      

  3.   

    declare @a table(id int)
    declare @b table(id int)insert @a
    select 1 union
    select 2 union
    select 3 union
    select 4 union
    select 5
    insert @b
    select 1 union
    select 2 union
    select 4 union
    select 5--查询
    select id from @a a
    where not exists(select 1 from @b where id=a.id)--结果
    /*id          
    ----------- 
    3(所影响的行数为 1 行)
    */
      

  4.   

    select * from  a  where a.id not in (select b.id from b)