Zhang Yongheng
Zhang Yongheng

Reputation: 125

Postgresql Combine two select statement with different number of columns

table1:

tconst attr_a attr_b attr_c

table2:

tconst attr_d attr_e

What I want:

tconst attr_a attr_b attr_c attr_d attr_e

Note: The tconst in table1 and table2 are different numbers, no intersection

Could anyone help? Thanks!

Upvotes: 0

Views: 82

Answers (1)

rohitvats
rohitvats

Reputation: 1851

You can try something like this, so that both statements in the union have the same number of columns:

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

Upvotes: 1

Related Questions