我有一张桌子,我必须加入4列:
SELECT columns
FROM table t
LEFT JOIN other_table ot ON o.col1 = ot.col1
AND o.col2 = ot.col2
AND o.col3 = ot.col3
AND o.col4 = ot.col4
但是,如果other_table中的任何列为null,我想将连接条件评估为true,而不管t中另一列的值如何,例如,如果给定行的ot.col4 IS为NULL,则仅评估col1 ,加入时的col2和col3条件。
有什么想法吗?
答案 0 :(得分:4)
请尝试:
SELECT columns
FROM table t
LEFT JOIN other_table ot ON o.col1 = NVL(ot.col1, o.col1)
AND o.col2 = NVL(ot.col2, o.col2)
AND o.col3 = NVL(ot.col3, o.col3)
AND o.col4 = NVL(ot.col4, o.col4)
答案 1 :(得分:2)
SELECT columns
FROM table t
LEFT JOIN other_table ot ON o.col1 = nvl(ot.col1,t.col1)
AND o.col2 = nvl(ot.col2, t.col2)
AND o.col3 = nvl(ot.col3, t.col3)
AND o.col4 = nvl(ot.col4, t.col4)