日期:2014-05-17  浏览次数:20714 次

求个简易的SQL
我有二个表结构一样的表,都有至少200左右的数据,我要查询出二个表中不同的记录,请教下怎么快些?

------解决方案--------------------
简单的话
假设id关联,A表纪录少
找出A表不存在,B表存在的纪录

select B.* from B
where not exists(select 'Z ' from A where A.id=B.id)

有个其他的方法
select A.id,B.id from A,B
where A.id(+)=B.id
and A.id is null

如果2表都可能缺少
select A.*, 'A ' from A
union
select B.*, 'B ' from B
minus
select A.*, 'AB ' from A,B
where A.id=B.id

没测试,仅工参考
------解决方案--------------------
上半部分是 b中有 而 a 中没有的记录
下半部分是 a中有 而 b 中没有的记录

用union 加起来 就是

两个表中 全部不同的记录的集合