PostgreSQL合并两个具有不同列数的选择语句

时间:2020-01-22 05:55:18

标签: sql postgresql

表1:

tconst attr_a attr_b attr_c

表2:

tconst attr_d attr_e

我想要什么:

tconst attr_a attr_b attr_c attr_d attr_e

注意: table1和table2中的tconst是不同的数字,没有交集

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试执行以下操作,以使union中的两个语句具有相同的列数:

select tconst , attr_a , attr_b , attr_c, null attr_d, null attr_e
from table1

union all

select tconst , null attr_a , null attr_b , null attr_c, attr_d, attr_e
from table2