多个表上有多个LEFT OUTER / INNER JOIN

时间:2018-02-16 10:47:36

标签: sql oracle

我想转换以下Oracle SQL查询语法(使用LEFT OUTER JOIN而不是(+)):

SELECT  *
FROM  TABLEA A,     TABLEC C,       TABLEE E,
            TABLEF F,   TABLEB B,   TABLEG G,   TABLEH H, 
            TABLEX XY,  TABLED D,   TABLEI I
WHERE XY.MYCOL = A.MYCOL

  AND B.X = A.X
  AND D.Y = A.Y
  AND D.Z = A.Z

 AND D.COL1 = C.COL1

  AND E.COL2 = C.COL2
  AND E.COL3 = C.COL3
  AND E.COL4 = C.COL4

  AND E.COL5 = D.COL5
  AND E.COL6 = D.COL6
  AND E.COL7 = D.COL7

  AND E.COL8 = F.COL8

  AND G.COL9 = D.COL9

  AND H.COL10(+) = E.COL10
  AND H.COL11(+) = E.COL11
  AND H.COL12(+) = E.COL12
  AND H.COL13(+) = E.COL13

  AND G.lid = 1

  AND I.COL14 = C.COL14
  AND I.COL15 = C.COL15
  AND I.COL16 = C.COL16
  AND I.COL17 = 'VAL'

这是我到目前为止所尝试的内容:

SELECT *
FROM TABLEA A JOIN TABLEB B ON B.X = A.X
JOIN TABLEXY XY ON XY.MYCOL = A.MYCOL
JOIN TABLEC C ON C ON C.Y = A.Y
JOIN TABLEC C ON C.Z = A.Z
JOIN TABLED D ON D.COL1 = C.COL1
JOIN TABLEE E ON E.COL2 = C.COL2
JOIN TABLEE E ON E.COL3 = C.COL3
JOIN TABLEE E ON E.COL4 = C.COL4

JOIN TABLEE E ON E.COL5 = D.COL5
JOIN TABLEE E ON E.COL6 = D.COL6
JOIN TABLEE E ON E.COL7 = D.COL7

JOIN TABLEF F ON E.COL8 = F.COL8
JOIN TABLEG G ON G.COL9 = C.COL9

LEFT OUTER JOIN TABLEH H ON H.COL10 = E.COL10
LEFT OUTER JOIN TABLEH H ON H.COL11 = E.COL11
LEFT OUTER JOIN TABLEH H ON H.COL12 = E.COL12
LEFT OUTER JOIN TABLEH H ON H.COL13 = E.COL13

AND F.lid = 1

JOIN TABLEI I ON I.COL14 = C.COL14
JOIN TABLEI I ON I.COL15 = C.COL15
JOIN TABLEI I ON I.COL16 = C.COL16
JOIN TABLEI I ON I.COL17 = 'VAL';

但是我收到以下错误:

ORA-00918: définition de colonne ambigu
00918. 00000 -  "column ambiguously defined"
*Cause:    
*Action:

我还想了解翻译此类查询的一般方法,因为我真的不了解它。

2 个答案:

答案 0 :(得分:1)

我相信你想要像

这样的东西
SELECT *
FROM TABLEA A JOIN TABLEB B ON B.X = A.X
JOIN TABLEXY XY ON XY.MYCOL = A.MYCOL
JOIN TABLEC C ON C ON C.Y = A.Y AND
   C.Z = A.Z
JOIN TABLED D ON D.COL1 = C.COL1
JOIN TABLEE E ON E.COL2 = C.COL2 AND
   E.COL3 = C.COL3 AND
   E.COL4 = C.COL4 AND
   E.COL5 = D.COL5 AND
   E.COL6 = D.COL6 AND
   E.COL7 = D.COL7
JOIN TABLEF F ON E.COL8 = F.COL8
JOIN TABLEG G ON G.COL9 = C.COL9
JOIN TABLEI I ON I.COL14 = C.COL14 AND 
  I.COL15 = C.COL15 AND 
  I.COL16 = C.COL16 AND
  I.COL17 = 'VAL'
LEFT OUTER JOIN TABLEH H ON H.COL10 = E.COL10 AND
  H.COL11 = E.COL11 AND   
  H.COL12 = E.COL12 AND
  H.COL13 = E.COL13 AND 
WHERE
  F.lid = 1

答案 1 :(得分:0)

您不需要为每个谓词重复JOIN <table> <alias> ON

SELECT *
FROM
     TABLEA A

JOIN TABLEB B ON B.X = A.X

JOIN TABLEXY XY ON XY.MYCOL = A.MYCOL

JOIN TABLEC C ON  C.Y = A.Y
              AND C.Z = A.Z

JOIN TABLED D ON D.COL1 = C.COL1

JOIN TABLEE E ON  E.COL2 = C.COL2
              AND E.COL3 = C.COL3
              AND E.COL4 = C.COL4
              AND E.COL5 = D.COL5
              AND E.COL6 = D.COL6
              AND E.COL7 = D.COL7

JOIN TABLEF F ON E.COL8 = F.COL8

JOIN TABLEG G ON G.COL9 = C.COL9

LEFT OUTER JOIN TABLEH H ON  H.COL10 = E.COL10
                         AND H.COL11 = E.COL11
                         AND H.COL12 = E.COL12
                         AND H.COL13 = E.COL13
                         AND F.lid = 1

JOIN TABLEI I ON  I.COL14 = C.COL14
              AND I.COL15 = C.COL15
              AND I.COL16 = C.COL16
              AND I.COL17 = 'VAL';

您只需在加入新表时重复JOIN个关键字...

FROM
  <table>
<JOIN>
  <table>
    ON <predicate>
<JOIN>
  <table>
    ON <predicate>

谓词可以像你需要的那样复杂。在您的情况下,它只是<equality> AND <equality> AND <equality> AND .....