首页 > 数据库 > Oracle > 低版本数据库中替代全连接功能的语句
创建表:
create table t1( aa number(4) not null,bb number(4));
create table t2( aa number(4) not null,cc number(4));
填入数据:
insert into t1 values(1,10);
insert into t1 values(2,20);
insert into t1 values(3,30);
insert into t1 values(3,30);
insert into t2 values(2,200);
insert into t2 values(3,300);
insert into t2 values(4,400);
insert into t2 values(4,400);
需要得到结果:
AA BB CC
---------- ---------- ----------
1 10
2 20 200
3 60 300
4 800
oracle低版本,即不支持全连接数据下的解决思路:先union all 得到所有的aa,然后用aa去连接两表即可。
select distinct t0.aa,t1.bb,t2.cc
from
(select t1.aa from t1 union all select aa from t2 ) t0,
(select aa,sum(bb) bb from t1 group by aa) t1,
(select aa,sum(cc) cc from t2 group by aa) t2
where
t0.aa=t1.aa(+)
and t0.aa=t2.aa(+);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17027328/viewspace-1035259/ ,如需转载,请注明出处,否则将追究法律责任。