select B.* from test.dbo.m1 A,test.dbo.m2 B where
(A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)这样可以选出B表中 三个字段都相等的记录,我想取一下补集,也就是B表中剩下的其他记录,SQL怎么写啊,谢谢大虾!
(A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)这样可以选出B表中 三个字段都相等的记录,我想取一下补集,也就是B表中剩下的其他记录,SQL怎么写啊,谢谢大虾!
from test.dbo.m1 A
right join test.dbo.m2 B
on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
select B.*
from B
left join A
on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
where B.(其他字段) is NULL
from test.dbo.m2 B
where NOT EXISTs(select 1 from test.dbo.m1 A where A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)
select B.*
from B
left join A
on A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历
where A.(其他字段) is NULL
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
要是全部B表中的数据还是用OUTER JOIN 参见Tony大哥!
from test.dbo.m2 B
where NOT EXISTS(select 1 from test.dbo.m1 A where A.姓名=B.姓名 and A.年龄 = B.年龄 and A.学历=B.学历)
这个得出来的正确,只不过还有一点不明,由于我m1与m2表的字段不完全相同,比方说m1表的字段还要多个 "性别"啊, 括号中列出的应该是m1表中符合条件的表项, 他和m2表中的字段数都不完全相同,为何能在m2表中正确的排除。