Oracle syntax left joins three or more tables

后端 未结 2 407
后悔当初
后悔当初 2021-01-14 18:23

I am trying to wrap my head around the old oracle Left Join syntax. It\'s fine with two tables:

FROM A, B
WHERE
A.Col = B.Col (+)

(Lets cal

相关标签:
2条回答
  • 2021-01-14 19:09

    (+) style outer joins are inferior in terms of functionality compared to standard ANSI OUTER JOINS (as well as readability IMHO), which Oracle supported since 9i.

    What you want is effectively

    FROM C
    LEFT JOIN A ON C.C = A.C
    LEFT JOIN B ON A.C = B.C
    

    Does it make sense now? To be honest, I don't quite understand your questions because how the above syntax works is really obvious to me...

    0 讨论(0)
  • 2021-01-14 19:19

    You can see it in a cascading way. However, the key is to look for those tables which are left and right joined within the same query. In this case, the order is different: the condition where the table is right joined is applied first. I hope the following diagram will shed some light on this:

    enter image description here

    You can also check the order of these joins by looking at the execution plan of the query:

    For Q1:

    select a.c a, b.c b, c.c c   from a, b, c  where a.c = b.c (+)    and
    c.c = a.c (+)
    
    ------------------------------------------------------------------------
    | Id  | Operation           | Name | E-Rows |  OMem |  1Mem | Used-Mem |
    ------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |      |        |       |       |          |
    |*  1 |  HASH JOIN OUTER    |      |      4 |  2168K|  2168K|  805K (0)|
    |*  2 |   HASH JOIN OUTER   |      |      4 |  2616K|  2616K|  981K (0)|
    |   3 |    TABLE ACCESS FULL| C    |      4 |       |       |          |
    |   4 |    TABLE ACCESS FULL| A    |      4 |       |       |          |
    |   5 |   TABLE ACCESS FULL | B    |      4 |       |       |          |
    ------------------------------------------------------------------------
    

    For Q2:

    select a.c a, b.c b, c.c c   from a, b, c  where c.c = a.c (+)    and
    a.c = b.c (+)
    
    ------------------------------------------------------------------------
    | Id  | Operation           | Name | E-Rows |  OMem |  1Mem | Used-Mem |
    ------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |      |        |       |       |          |
    |*  1 |  HASH JOIN OUTER    |      |      4 |  2168K|  2168K|  801K (0)|
    |*  2 |   HASH JOIN OUTER   |      |      4 |  2616K|  2616K|  983K (0)|
    |   3 |    TABLE ACCESS FULL| C    |      4 |       |       |          |
    |   4 |    TABLE ACCESS FULL| A    |      4 |       |       |          |
    |   5 |   TABLE ACCESS FULL | B    |      4 |       |       |          |
    ------------------------------------------------------------------------
    
    0 讨论(0)
提交回复
热议问题