条件连接有问题

时间:2014-12-19 23:19:58

标签: sql database oracle11g

我正在尝试加入,具体取决于table1.column1null还是null

例如,我有两个表,table1table2,以及查询:

SELECT
    A.column2
FROM table1 A , table2 B
WHERE if A.column1 is not null then (A.column1=B.column1)
      else if A.column1 is  null then (A.column3 = B.column1);

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
    A.column2
FROM table1 A
JOIN table2 B ON
    B.column1 = A.column1 OR
    (A.column1 IS NULL AND B.column1 = A.column3)

请注意,如果B.column1 = A.column1B.column1A.column1NULL将永远不会为真。

答案 1 :(得分:0)

试试这个......

SELECT A.column2
FROM table1 A 
    JOIN table2 B ON NVL(A.column1, A.column3) = b.Column1

如果b.Column1也可以为null,并且你想比较两个空列,你可以尝试这个......

SELECT A.column2
FROM table1 A 
    JOIN table2 B ON NVL(b.Column1, 'X') = COALESCE(A.column1, A.column3, 'X') 

假设b.Column1永远不会有值' X'