我的一个 sql 中有一段代码是这样的:
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:已更新以显示更多查询。我已经转换了所有其他连接,只是缺少要转换的最后一行。
最佳答案
(+)
用于指示 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
关于sql - 如何将旧的连接代码行转换为新的连接语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46814708/