将列设置为Null并在Postgresql中联接 [英] Setting Column to Null and Joining in Postgresql

查看:385
本文介绍了将列设置为Null并在Postgresql中联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个表。一个表包含两列。第二个表包含一列。但是,第二个表中的列与第一个表中第一列的列名称匹配:

So I have two tables. One table contains two columns. The second table contains one column. However the column in the second table matches the column name of the first column of the first table:

Table 1:
a       b
hello | 6
world | 7

Table 2:
a
new
t1

我想加入这两个表以得出:

I want to join these two tables to result in:

Table 3:
a         b
hello  | 6
world  | 7
new    | Null
t1     | Null

我研究了Cross Join和其他许多Join,但似乎都没有帮助

I have looked into Cross Join, and many other Joins but none of them seem to help in this case.

推荐答案

SELECT a, b FROM table_1
UNION ALL
SELECT a, NULL::INTEGER FROM table_b
;

对于某些其他数据库,不需要将上述类型转换为整数。

With some other databases, the cast to integer as above is not necessary.

这篇关于将列设置为Null并在Postgresql中联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆