select B.* from test.dbo.m1 A,test.dbo.m2 B where 
(A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)这样可以选出B表中 三个字段都相等的记录,我想取一下补集,也就是B表中剩下的其他记录,SQL怎么写啊,谢谢大虾!

解决方案 »

  1.   

    select * 
    from test.dbo.m1 A
      right join test.dbo.m2 B 
         on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
      

  2.   


    select B.* 
    from  B
    left join A 
    on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
    where B.(其他字段) is NULL
      

  3.   

    select B.* from test.dbo.m2 B where NOT EXISTs(select 1 from test.dbo.m1 A where A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历) frp
      

  4.   

    select B.*
     from test.dbo.m2 B 
    where NOT EXISTs(select 1 from test.dbo.m1 A where A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)
      

  5.   

    TONy哥这样是全部查出来了,参考3,5
      

  6.   

    3楼不对吧,B表位主表,B表中能有null值么?
      

  7.   


    select B.* 
    from  B
    left join A 
    on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
    where A.(其他字段) is NULL
      

  8.   

    如果两表结构完全一样, 或者只需要姓名, 年龄, 学历的信息, 那就用EXCEPT吧. drop table A
    drop table Bcreate table m1
    (姓名 nvarchar(32),
     年龄 int,
     学历 nvarchar(32),
     其它 nvarchar(32)
    )create table m2
    (姓名 nvarchar(32),
     年龄 int,
     学历 nvarchar(32),
     其它 nvarchar(32)
    )insert m1 (姓名, 年龄, 学历, 其它)
    select 'A', 1, 'S', '1'
    union all select 'B', 1, 'L', '2'
    union all select 'C', 1, 'L', '3'
    insert m2 (姓名, 年龄, 学历, 其它)
    select 'A', 1, 'S', '1'
    union all select 'B', 1, 'L', '2'
    union all select 'C', 1, 'L', '3'
    union all select 'D', 1, 'L', '3'select 姓名, 年龄, 学历
    from m2
    except
    select 姓名, 年龄, 学历
    from m1
      

  9.   

    要是取补集的话,还是这个!
    要是全部B表中的数据还是用OUTER JOIN 参见Tony大哥!
      

  10.   

    select B.*
    from test.dbo.m2 B 
    where NOT EXISTS(select 1 from test.dbo.m1 A where A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)
      

  11.   


    这个得出来的正确,只不过还有一点不明,由于我m1与m2表的字段不完全相同,比方说m1表的字段还要多个 "性别"啊,  括号中列出的应该是m1表中符合条件的表项, 他和m2表中的字段数都不完全相同,为何能在m2表中正确的排除。