PCG Legacy
PCG Legacy

Reputation: 1

UNION ALL with different number of columns and creating Nulls MySQL

I am trying to create a query that will UNION ALL a set of historical data and a set of new data that have a different number of columns. I want it to UNION ALL on all matching columns and create a new column in the historical data set as NULL if the new data has a new column.

Here is my current code I know is wrong:

SELECT * FROM `historical_data`

UNION ALL

SELECT * FROM `new_data`

Upvotes: 0

Views: 67

Answers (1)

Akina
Akina

Reputation: 42641

For example, if fields 1 and 4 are present in both tables, and fields 2 and 3 in new table only:

SELECT field1, field2, field3, field4 FROM `new_data`
UNION ALL
SELECT field1,  NULL ,  NULL , field4 FROM `historical_data`

Upvotes: 1

Related Questions