我有以下表格
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