有没有办法在oracle中做多个左外连接?

时间:2009-10-23 17:03:00

标签: sql oracle

为什么这不适用于Oracle?

有没有办法让这项工作?

FROM table1 a,
     table2 b,
     table3 c
WHERE a.some_id = '10'
AND a.other_id (+)= b.other_id
AND a.other_id (+)= c.other_id

我希望table1在多个表上保持外连接...

如果我尝试将其更改为使用ANSI连接,则会出现编译错误。我做了以下事情:

FROM table2 b, table3 c
LEFT JOIN table1 a ON a.other_id = b.other_id and a.other_id = c.other_id

6 个答案:

答案 0 :(得分:7)

好的,看看Oracle文档中的examples,我对语法的回忆是正确的,所以我将我的评论转化为答案。假设您的目标是左外连接,其中A是基表,并且您连接来自B和C的匹配行,请按如下方式重写您的查询(请注意,我只是更改前缀;我希望将源行设置为右边)。

FROM table1 a,
     table2 b,
     table3 c
WHERE a.some_id = '10'
AND b.other_id (+)= a.other_id
AND c.other_id (+)= a.other_id

如果那不是你想要做的,那么查询就是borked:你正在进行B和C的笛卡尔连接,然后尝试从该部分结果到A的外连接,并附加一个谓词A.这没有多大意义。

答案 1 :(得分:5)

使用ansi加入。他们的IMO更清晰。由于某些原因他们不能使用物化视图...

答案 2 :(得分:2)

你可以这样做。

FROM table1 a,     table2 b,     table3 c
WHERE a.some_id = '10'
AND a.other_id = b.other_id(+)
AND a.other_id = c.other_id(+)

答案 3 :(得分:2)

我想分别解决你问题的这一部分:

If I try to change it to ANSI join I get compilation errors. I did the following:

FROM table2 b, table3 c
LEFT JOIN table1 a ON a.other_id = b.other_id and a.other_id = c.other_id

在ANSI连接中,至少在Oracle中,您正在运行两个行源。示例中的LEFT JOIN运算符将table3和table1作为其操作数;所以你不能在ON子句中引用“b.otherid”。每个附加表都需要一个新的连接运算符。

我相信你要做的是外连接表2和表3到表1.所以你应该做的是:

FROM table1 a LEFT JOIN table2 b ON b.other_id = a.other_id
              LEFT JOIN table3 c ON c.other_id = a.other_id

或Henry Gao的查询是否要使用特定于Oracle的语法。

答案 4 :(得分:0)

在oracle中,您不能将同一个表外连接到多个其他表。您可以创建具有联接的视图,然后加入该视图。作为旁注,您也不能外连接到子选择,因此这里也不是一个选项。

答案 5 :(得分:0)

您可以尝试以下方法(表b和c是BASE) FROM(SELECT other_id FROM table2       联盟       SELECT other_id FROM table3)b LEFT JOIN table1 a b.other_id = a.other_id

但是我再次成为Oracle Nono