读书人

求简单ms-sql语句,该怎么处理

发布时间: 2012-04-25 19:32:32 作者: rapoo

求简单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,xjselect * from bexceptselect * from a--如果b包含a,可以select *from b twhere 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 twhere not exists (select 1 from a where dm=t.dm and ztz=t.ztz and sj=t.sj and xj=t.xj)unionselect *from a twhere not exists (select 1 from b where dm=t.dm and ztz=t.ztz and sj=t.sj and xj=t.xj) 

读书人网 >SQL Server

热点推荐