steve.g
steve.g

Reputation: 115

Combining 3 tables into one table

I have three tables that have the exact same columns, the only difference is the data in those columns. I was wondering if (and how) I should merge the columns into one table. I know that the data would be distinguishable by a boolean value if you merge two tables, but can the same be done with three tables?

Any help would be appreciated.

Upvotes: 0

Views: 745

Answers (1)

Taryn
Taryn

Reputation: 247690

If the columns and datatypes are the same, then you can use a UNION ALL query.

select col1, col2, 'table1' as src
from table1
union all
select col1, col2, 'table2' as src
from table2
union all
select col1, col2, 'table3' as src
from table3

This version will include any duplicate records, if you don't want duplicates then you can use UNION which will remove any duplicated records.

If the datatypes are not the same, then you will need to convert the data to be of the same type.

Upvotes: 4

Related Questions