MSSQL中有两个表,结构一样,B中有的A中都有,A中有的B中不一定有。A、B表中部门、姓名都设为主键。
表A:
部门    姓名
物流    张三
物流    李四
财务    赵武
采购    刘三
采购    韩起
人事    王五表B:
部门    姓名
物流    张三
采购    刘三
财务    赵武现在要使用select * from A语句查询表A中存在但表B中没有的记录,如何写?

解决方案 »

  1.   


    --SQL2000select *
    from A
    where not exists (select 1 from b where 部门 = a.部门 and 姓名 = a.姓名)--SQL2005select * from a
    except
    select * from b
      

  2.   


    --2000  or  2005select a.*
    from a join b on a.部门 = b.部门 and a.姓名 = b.姓名
    where b.部门 is null 
      

  3.   

    select *
    from a
    where not exists (select 1 from b where b.部门 = a.部门 and b.姓名 = a.姓名)
    union all
    select *
    from b
    where not exists (select 1 from a where b.部门 = a.部门 and b.姓名 = a.姓名)
      

  4.   

    --想多了,像小三这样就可以了
    select *
    from a
    where not exists (select 1 from b where b.部门 = a.部门 and b.姓名 = a.姓名)
      

  5.   


    B中有的A中都有,A中有的B中不一定有豆子要看好,有点冗余哦!