Oracle内部连接在多个表上返回空值

时间:2014-11-20 05:10:51

标签: oracle

我正在处理一个sql内连接查询,该查询应从其他表返回MAX元素。表B,C,D,E可能为空。在这种情况下,也应该在输出中打印null。

这就是我的尝试。

SELECT A.INDEX, A.CODE, MAX(B.DAY), MAX(C.TIMESTAMP), MAX(D.TIMESTAMP) 
FROM A 
INNER JOIN B 
INNER JOIN C
INNER JOIN D
INNER JOIN E
ON A.INDEX = B.INDEX
ON A.INDEX = C.INDEX
ON A.INDEX = D.INDEX
ON A.INDEX = E.INDEX AND E.FUNCTION = 0;

表定义:

A
-------
INDEX NOT NULL NUMBER(10)
CODE NOT NULL VARCHAR2(16)

B
--------
INDEX NUMBER(10)
DAY NUMBER(10)

C
---------
INDEX NUMBER(10)
TIMESTAMP TIMESTAMP(6)

D
---------
INDEX NUMBER(10)
TIMESTAMP TIMESTAMP(6)

E
----------
INDEX NUMBER(10)
FUNCTION NUMBER(5)

1 个答案:

答案 0 :(得分:4)

在这种情况下,请考虑使用LEFT OUTER JOIN代替

FROM A 
LEFT JOIN B 

所以你的情况

SELECT A.INDEX, A.CODE, MAX(B.DAY), MAX(C.TIMESTAMP), MAX(D.TIMESTAMP) 
FROM A 
LEFT JOIN B ON A.INDEX = B.INDEX
LEFT JOIN C ON A.INDEX = C.INDEX
LEFT JOIN D ON A.INDEX = D.INDEX
LEFT JOIN E ON A.INDEX = E.INDEX 
AND E.FUNCTION = 0;