如何将旧的连接代码行转换为新的连接语法?

时间:2017-10-18 16:14:33

标签: sql oracle outer-join

我的一个sqls中有一段代码如下:

FROM 
Table1
LEFT OUTER JOIN
              TABLE2
           ON (Table1.Field1 = Table2.Field1))
    LEFT OUTER JOIN
        TABLE3
       ON(Table2.Field1 = Table3.Field1))
    LEFT OUTER JOIN
        Table4
       ON(Table3.Field1 = Table4.Field1))
    LEFT OUTER JOIN 
        Table5
       ON(Table4.Field1= Table5.Field1)

Table6.Field1(+) = 'Y' (How do I convert this?)

我知道这是一个正确的外连接,语法会更新为今天的现代语法吗?

Edit1:已更新以显示更多查询。我已经转换了所有其他连接,只是错过了要转换的最后一行。

1 个答案:

答案 0 :(得分:1)

(+)用于指示outer join,因为也允许不匹配的行。但是,您与我们分享的代码段不包含有关“table6”的足够信息

根据连接t1到t2,t2到t3,t3到t4,t4到t5的进展,我的猜测是t5到t6,如下所示:

FROM Table1
LEFT OUTER JOIN TABLE2 ON Table1.Field1 = Table2.Field1
LEFT OUTER JOIN TABLE3 ON Table2.Field1 = Table3.Field1
LEFT OUTER JOIN Table4 ON Table3.Field1 = Table4.Field1
LEFT OUTER JOIN Table5 ON Table4.Field1 = Table5.Field1
LEFT OUTER JOIN Table6 ON Table5.Field1 = Table6.Field1
         AND Table6.Field1 = 'Y'
CREATE TABLE Table1    (TBL VARCHAR2(2), FIELD1 VARCHAR2(1));
INSERT INTO Table1 (tbl, field1)          VALUES ('t1', 'y');
INSERT INTO Table1 (tbl, field1)          VALUES ('t1', 'n');
INSERT INTO Table1 (tbl, field1)          VALUES ('t1', 'y');
INSERT INTO Table1 (tbl, field1)          VALUES ('t1', 'n');
CREATE TABLE Table6    (TBL6 VARCHAR2(2), FIELD1 VARCHAR2(1));
INSERT INTO Table6 (tbl6, field1)         VALUES ('t6', 'y');
INSERT INTO Table6 (tbl6, field1)         VALUES ('t6', 'n');
INSERT INTO Table6 (tbl6, field1)         VALUES ('t6', 'y');
INSERT INTO Table6 (tbl6, field1)         VALUES ('t6', 'n');

旧语法

select table1.tbl, table1.field1, table6.tbl6, table6.field1 as t6_field1
from table1, table6
where table1.field1 = table6.field1(+)
and table6.field1(+) = 'y';
TBL | FIELD1 | TBL6 | T6_FIELD1
:-- | :----- | :--- | :--------
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | n      | null | null     
t1  | n      | null | null     

新语法

select table1.tbl, table1.field1, table6.tbl6, table6.field1 as t6_field1
from table1
left join table6 on table1.field1 = table6.field1
    and table6.field1 = 'y';
TBL | FIELD1 | TBL6 | T6_FIELD1
:-- | :----- | :--- | :--------
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | y      | t6   | y        
t1  | n      | null | null     
t1  | n      | null | null     

dbfiddle here