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

求一个高效率的sql语句
我有两个表,其中A表查询结果如下:

编号 类型



10813 申请表
10813 登记证
10813 执照
10813 证明文件
10813 机构代码证


10827 登记证
10827 资信证明
10827 执照
10827 证明文件
10827 机构代码证

B表查询结果如下:

编号 类型

10798 执照
10798 资信证明
10798 申请表
10798 登记证
10798 机构代码证
10798 证明文件



10803 申请表
10803 机构代码证
10803 登记证
10803 证明文件
10803 资信证明
10803 执照

我想查询编号为10803中A表和B表内容不一样的部分

查询结果为:

10803 资信证明

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

with a as(
select 10813 id,'申请表' type from dual union all
select 10813 id,'登记证' type from dual union all
select 10813 id,'执照' type from dual union all
select 10813 id,'证明文件' type from dual union all
select 10813 id,'机构代码证' type from dual union all
select 10827 id,'登记证' type from dual union all
select 10827 id,'资信证明' type from dual union all
select 10827 id,'执照' type from dual union all
select 10827 id,'证明文件' type from dual union all
select 10827 id,'机构代码证' type from dual
),
b as(
select 10798 id,'执照' type from dual union all
select 10798 id,'资信证明' type from dual union all
select 10798 id,'申请表' type from dual union all
select 10798 id,'登记证' type from dual union all
select 10798 id,'机构代码证' type from dual union all
select 10798 id,'证明文件' type from dual union all
select 10813 id,'申请表' type from dual union all
select 10813 id,'机构代码证' type from dual union all
select 10813 id,'登记证' type from dual union all
select 10813 id,'证明文件' type from dual union all
select 10813 id,'资信证明' type from dual union all
select 10813 id,'执照' type from dual
)
select *
from ((select a.id,a.type from a 
      minus
      select b.id,b.type from b)
      union all
      (select b.id,b.type from b
      minus
      select a.id,a.type from a)) t
where id=10813;

        ID TYPE
---------- ----------
     10813 资信证明