特殊的Oracle外连接语法案例


16

我在应该从Oracle外连接语法移植到SQL标准外连接语法的查询中看到了以下内容:

SELECT ...
FROM A, B, C, D, E
WHERE A.A_ID = B.A_ID
AND B.B_ID = C.A_ID(+)
AND B.B_KEY = C.B_KEY(+)
AND C.C_ID = D.C_ID(+)
AND B.A_ID = E.A_ID(+)
AND B.B_KEY = E.B_KEY(+)
AND 'CONSTANT' = C.X_ID(+)

现在,翻译外部联接语法通常是一个机械过程,但是最后一行让我感到困惑。这是什么意思?它有什么作用?

Answers:


11

我试图执行机械过程。我希望我没记错。

这导致:

SELECT ...
FROM A
         join B on A.A_ID = B.A_ID
    left join C on B.B_ID = C.A_ID and B.B_KEY = C.B_KEY and 'CONSTANT' = C.X_ID
    left join D on C.C_ID = D.C_ID
    left join E on B.A_ID = E.A_ID and B.B_KEY = E.B_KEY

简而言之,我认为Leigh Riffel的答案是正确的。

注意

在过去,要记住的规则是:oracle,其中Aa = Bb(+)变为Aa * = Bb在SQL-Server旧语法中,加号移到相反的一侧并成为星形,这意味着A在Aa = Bb上左移连接B


10

该行要求c.X_ID等于常数值,或者C表中没有记录。当然,由于它保持联接状态,因此它不会限制A表中的记录,而只会限制C表中已联接的记录。这是一个示范:

设定:

CREATE TABLE T1 as (select rownum+1 t1_id from dual connect by rownum <= 4);
CREATE TABLE T2 as (
   select rownum t1_id, DECODE(rownum,2,'CONSTANT',3,'NoMatch') CompareField 
   from dual connect by rownum <= 3
);
SELECT * FROM T1;
SELECT * FROM T2;

结果:

SELECT T1.t1_id, T2.t1_id, T2.CompareField
FROM T1, T2
WHERE T1.t1_id = T2.t1_id(+)
AND 'CONSTANT' = T2.CompareField(+)
ORDER BY 1;

要么:

SELECT T1.t1_id, T2.t1_id, T2.CompareField
FROM T1 LEFT JOIN T2 ON T1.t1_id = T2.t1_id 
AND 'CONSTANT' = T2.CompareField
ORDER BY 1;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.