3

我试图通过使用这些简单的表来理解完全外连接

t1
-----------
 a  | b
===========
 1  | 2
----------- 

t2
------------
  b  |  c
===========
  2  | 3
------------

t3
-------------
  a  |  c
=============
  4  |  5
-------------

To understand select * from t1 natural full outer join (t2 natural full outer join t3);

我先试过select * from t2 natural full outer join t3 让我称之为result

-----------------
  a  |  b  |  c
=================
     | 2   | 3
----------------
  4  |     | 5
------------------

然后我尝试了

select * from t1 自然全外连接(结果)

    t1             result
-----------      -----------------
 a  | b           a    |  b  | c
===========      =================
 1  | 2                | 2   | 3
-----------      ----------------
                    4  |     | 5
                 ------------------

这不应该是

--------------------------
   a    |  b  |  c
===========================
   1    |  2  | 3
-------------------------
  4     |     | 5

我不太明白 sql 查询如何给出

 a | b | c 
---+---+---
 4 |   | 5
   | 2 | 3
 1 | 2 | 
4

1 回答 1

1

您可以使用UNION ALL指定 ALL 以保留重复项)来执行此操作。

SELECT  a, b, NULL as c FROM t1
UNION ALL
SELECT  NULL as a, b, c FROM t2
UNION ALL
SELECT  a, NULL as b, c FROM t3
于 2013-03-04T09:18:01.530 回答