当前位置: 代码迷 >> Sql Server >> sql 的多表查询有关问题
  详细解决方案

sql 的多表查询有关问题

热度:42   发布时间:2016-04-24 09:55:43.0
sql 的多表查询问题
我有A,B两个表,B表是根据A表更新过的,我现在想选出A,B两个表中的不同的数据,这两个表是以dept_no和cust_no共同作为主键的:

怎么破?
------解决思路----------------------
查询A表的数据不存在B表

select A.* from A left join B on A.dept_no=B.dept_no and A.cust_no=B.cust_no where B.cust_no is null

查询B表的数据不存在A表

select B.* from A right join B on A.dept_no=B.dept_no and A.cust_no=B.cust_no where A.cust_no is null

------解决思路----------------------
你更新 直接更新着2列 还是后面的其他列内容?直接更新主键 很容易报错啊。
查询2个表中不同的数据直接
(select * from a 
except 
select * from b)
union all
(select * from b
except 
select * from a)
  相关解决方案