简体   繁体   English

使用Oracle外部联接链接多个表

[英]Linking multiple tables with an Oracle outer join

Struggling with Oracle outer join syntax. 苦苦挣扎的Oracle外连接语法。

We have this query with inner and outer joins; 我们使用内部联接和外部联接进行查询。

SELECT A.aa, B.bb, C.cc, D.dd
FROM
  TABLEA A, TABLEB B, TABLEC C, TABLED D
WHERE
  A.XX = B.XX AND
  B.YY = C.YY AND
  C.ZZ = D.WW (+)

The query works fine. 该查询工作正常。 A change is now it's possible that the link between table A and B (on XX) may not be present. 现在进行了更改,可能是表A和表B之间的链接(在XX上)可能不存在。

So we'd like to turn this into an outer join which returns data regardless of whether the existing joins are satisfied OR if there is no link between A and B (and the other tables). 因此,我们希望将其转换为外部联接,该联接将返回数据,而不管现有联接是否满足,或者A和B(以及其他表)之间是否没有链接。

How can you do this? 你该怎么做?

Say you have your tables like the following: 假设您的表格如下:

insert into tableA values (1);
insert into tableA values (2);
insert into tableB values ( 1, 10);
insert into tableB values ( -2, 20);
insert into tableC values ( 10, 100);
insert into tableC values ( 20, 200);
insert into tableD values ( 200);
insert into tableD values ( 999);

If I understand well, you need to use on outer join even on B and C, not only D; 如果我了解得很好,那么您甚至需要在B和C上使用外部联接,而不仅仅是D; in the old Oracle syntax this is: 在旧的Oracle语法中,这是:

SELECT *
FROM
  TABLEA A, TABLEB B, TABLEC C, TABLED D
WHERE
  A.XX = B.XX(+) AND
  B.YY = C.YY(+) AND
  C.ZZ = D.WW (+)

And in (better) ANSI SQL: 在(更好的)ANSI SQL中:

select *
from tableA A
       left outer join
     tableB B on ( A.xx = B.xx)
       left outer join 
     tableC C on ( B.yy = C.yy)
       left outer join
     tableD D on ( C.zz = D.ww)

They both give: 他们都给:

        XX         XX         YY         YY         ZZ         WW
---------- ---------- ---------- ---------- ---------- ----------
         2
         1          1         10         10        100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM