我想从两个表中得到结果,即使每个其他表中不存在相应的行。我应该使用哪种连接来获得如下结果。我试过了,但没有运气。
表1:
CA CB CC
=== === ===
AA 11 X1
BB 22 X2
CC 33 X3
EE 44 X4
表2:
CA CB CD
=== === ===
AA 11 Y1
BB 22 Y2
FF 55 Y5
GG 66 Y6
期待结果如:
CA CB CC CD
=== === === ===
AA 11 X1 Y1
BB 22 X2 Y2
CC 33 X3 (null)
EE 44 X4 (null)
FF 55 (null) Y5
GG 66 (null) Y6
答案 0 :(得分:2)
SELECT
COALESCE (t1.CA, t2.CA) CA
,COALESCE (t1.CB, t2.CB) CB
,CC
,CD
FROM Table1 t1
FULL JOIN Table2 t2 ON t1.CA = t2.CA AND t1.CB = t2.CB
ORDER BY
COALESCE (t1.CA, t2.CA)
,COALESCE (t1.CB, t2.CB)