Reputation: 37
I know the topic is already on other threads, but my problem is that i could not use union (table 1 has 60 columns; table 2 has only 7). Is there another way than creating for table 2 ...53 empty columns?
Is it possible to generate the result in one query?
Thank you!
Upvotes: 1
Views: 1352
Reputation: 34304
It is possible, since you can add any number of arbitrary columns in a select:
select field1, field2, field3 from table1
union
select field4, null, field5 from table2
In the above example I used a constant null value as the 2nd field, but you can choise any value befitting the data type of the existing column in the other table.
Upvotes: 0
Reputation:
You can simply do this by replacing non existent columns with nulls like below
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
Replace the columns in tables with null if the column does not exist.
Upvotes: 2