SQL-连接多个表后的自定义选择视图

时间:2018-11-02 20:28:22

标签: sql join select view union

一个非常简单的问题,我似乎无法弄清楚。

我有下面两个表。我想编写一个选择查询,该查询返回以下

| UID  | NAME    | DESCRIPTION | CC_CONFIG_UID |
├------+---------+-------------+---------------┤
| xxx  | HELLO_1 | NULL        | abc           |
| yyy  | WORLD_1 | NULL        | hij           |
| aaa  | NULL    | HELLO_2     | efg           |
| bbb  | NULL    | WORLD_2     | klm           |

表A:

| UID  | NAME    | CC_CONFIG_UID |
|------+---------+---------------|
| xxx  | HELLO_1 | abc           |
| yyy  | WORLD_1 | hij           |

表B:

| UID  | DESCRIPTION | CC_CONFIG_UID |
|------+-------------+---------------|
| aaa  | HELLO_2     | efg           |
| bbb  | WORLD_2     | klm           |

我尝试过

(SELECT * FROM A) UNION (SELECT * FROM B)

但是我得到以下回报,这很接近但不是我所需要的:

| UID  | NAME    | CC_CONFIG_UID |  
├------+---------+---------------┤
| xxx  |HELLO_1  |  abc          |
| aaa  |HELLO_2  |  def          |
| yyy  |WORLD_1  |  hig          |
| bbb  |WORLD_2  |  klm          |

1 个答案:

答案 0 :(得分:1)

您需要指定列。您的表有三列,但您希望结果集有四列:

select a.uid, a.name, null as description, a.cc_config_uid
from a
union all
select b.uid, null as name, bdescription, b.cc_config_uid
from b;