2013-11-21 46 views
0

我有以下表甲骨文接合,從而獲得所需結果

Table A    Table B    Table C 
ColumnA ColumnB  ColumnA ColumnB  ColumnA ColumnB 
1  A     2 X    X  Value1 
2  B     3 Y    Y  Value2 
3  C     5 Z    Z  Value3 
4  D 
5  E 

所需要的結果是

Column1 Column2 Column3 

1    A 
2  Value1 B 
3  Value2 C 
4    D 
5  Value3 E 

我一直在用左手演奏外聯接。但仍然沒有接近我期待的結果。任何幫助表示讚賞。

+0

那你試試用左外連接和結果是什麼? –

回答

1

您需要使用LEFT JOIN兩次:

CREATE table tablea (
    columna NUMBER, 
    columnb VARCHAR2(1) 
); 

CREATE table tableb (
    columna NUMBER, 
    columnb VARCHAR2(1) 
); 

CREATE table tablec (
    columna VARCHAR2(1), 
    columnb VARCHAR2(10) 
); 

INSERT INTO tablea VALUES (1, 'A'); 
INSERT INTO tablea VALUES (2, 'B'); 
INSERT INTO tablea VALUES (3, 'C'); 
INSERT INTO tablea VALUES (4, 'D'); 
INSERT INTO tablea VALUES (5, 'E'); 

INSERT INTO tableb VALUES (2, 'X'); 
INSERT INTO tableb VALUES (3, 'Y'); 
INSERT INTO tableb VALUES (5, 'Z'); 

INSERT INTO tablec VALUES ('X', 'Value1'); 
INSERT INTO tablec VALUES ('Y', 'Value2'); 
INSERT INTO tablec VALUES ('Z', 'Value3'); 

COMMIT; 

SELECT ta.columna, tc.columnb, ta.columnb 
    FROM tablea ta 
    LEFT JOIN tableb tb ON (ta.columna = tb.columna) 
    LEFT JOIN tablec tc ON (tc.columna = tb.columnb) 
ORDER BY 1  
; 

輸出:

 COLUMNA COLUMNB COLUMNB 
---------- ---------- ------- 
     1   A  
     2 Value1  B  
     3 Value2  C  
     4   D  
     5 Value3  E 

SQLFiddle demo