日期:2014-05-18  浏览次数:20396 次

求简单ms-sql语句
数据表A(dm,ztz,sj,xj),数据表B(dm1,ztz1,sj1,xj1),表B记录比A更多,求一句简单代码,检索出表A和B对应字段不相等所有记录,即使dm!=dm1或ztz!=ztz1或sj!=sj1或xj!=xj1 的所有记录。

------解决方案--------------------
SQL code

--dm,ztz,sj,xj
select * from b
except
select * from a

--如果b包含a,可以

select *
from b t
where not exists (select 1 from a where dm=t.dm and ztz=t.ztz and sj=t.sj and xj=t.xj)

--如果b和a不是互相包含的
select *
from b t
where not exists (select 1 from a where dm=t.dm and ztz=t.ztz and sj=t.sj and xj=t.xj)
union
select *
from a t
where not exists (select 1 from b where dm=t.dm and ztz=t.ztz and sj=t.sj and xj=t.xj)