ITPub博客

首页 > 数据库 > Oracle > 低版本数据库中替代全连接功能的语句

低版本数据库中替代全连接功能的语句

原创 Oracle 作者:ty1921 时间:2010-07-14 15:30:27 0 删除 编辑

创建表:

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(+);

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17027328/viewspace-1035259/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2012-02-17

  • 博文量
    1
  • 访问量
    3822