-1

我想从两个表中获取结果,即使对应的行不存在于其他表中。我应该使用哪个连接来获得如下结果。我试过FULL,但没有运气。

表格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
4

1 回答 1

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) 

SQLFiddle

于 2013-04-08T22:10:11.997 回答