這個問題在這里已經有了答案: PostgreSQL 中 LEFT OUTER JOIN 的“WHERE”語法等效 1 個答案 10 小時前關閉。
我在 Oracle 中有以下原始查詢,
select ...
FROM nvision_trainees_t nv, plans_t p, lookup_t idp_look
WHERE nv.ned_id = p.trainee_ned_id( )
AND p.idp_type_id = idp_look.id( )
AND (
(p.id IS NULL AND nv.organizationalstat IN ('EMPLOYEE', 'FELLOW'))
OR
p.id IS NOT NULL
);
此查詢已遷移到( )不起作用的 Postgres。我不得不相應地重寫它。因為它在等式的右邊,所以它暗示RIGHT OUTER JOIN。所以我重寫了如下查詢,但它是錯誤的。我認為原因是,最后一個條件成為了的一部分,RIGHT OUTER JOIN而最初它是內部連接的一部分。它沒有給出相同的結果。
select ...
from nvision_trainees_t nv
right outer join plans_t p on p.trainee_ned_id = nv.ned_id
right outer join lookup_t idp_look on
idp_look.id = p.idp_type_id
/* I feel that this is wrong somehow, it became part of the RIGHT OUTER JOIN condition */
and ((p.id is null and nv.organizationalstat in ('EMPLOYEE', 'FELLOW'))
or p.id is not null);
有關如何使用 LEFT/RIGHT INNER/OUTER JOIN 正確重寫原始查詢的任何提示?
UPDATE After I tried JasonTrue's suggestion of separating the bottom clause as its own WHERE, it worked with the original LEFT JOIN, not the re-written RIGHT JOIN, for some reason. In other words, this worked, does anyone know why? Was I on the wrong track with doing a RIGHT OUTER JOIN for the right-hand ( )?
from nvision_trainees_t nv
left outer join plans_t p on p.trainee_ned_id = nv.ned_id
left outer join lookup_t idp_look on idp_look.id = p.idp_type_id
where((p.id is null and nv.organizationalstat in ('EMPLOYEE', 'FELLOW'))
or p.id is not null);
uj5u.com熱心網友回復:
(由OP補充:右手( )確實是a LEFT JOIN,所以不應該改成,RIGHT JOIN而是問題如下)
在您的 Oracle 示例中,您的最終“AND”子句與 JOIN 操作無關。在您的 Postgres 中,您將最后的 AND 子句作為連接條件的一部分。
我認為您只希望最后一個子句作為查詢過濾器的一部分,而不是連接條件的一部分。這是一個例子:
select ...
from nvision_trainees_t nv
LEFT outer join plans_t p on p.trainee_ned_id = nv.ned_id
LEFT outer join lookup_t idp_look on
idp_look.id = p.idp_type_id
WHERE ((p.id is null and nv.organizationalstat in ('EMPLOYEE', 'FELLOW'))
or p.id is not null);
我沒有對此進行測驗,但在 ANSI SQL 中,WHERE 子句通常出現在連接之后,所以我認為它等同于您的原始查詢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438768.html
標籤:sql postgresql oracle
上一篇:從其他表中查找具有組值的行
