KSp
KSp

Reputation: 1229

Union of multiple tables with different where condition

I have a few tables and I would like to do a union of all tables to get the date column but this needs to be especially site level and id level.

One of those tables (Table1 and Table2) is missing site_id and code so I am wondering what is the best way to do a union of all tables?

Table1:
column name
id
site_id
xx
date

Table2:
column name
id
yy
date

Table3:
column name
id
site_id
code
zz
date

Table4:
column name
id 
site_id
code
mm
date

It is better to create three different union query or we can do this in one query? Thanks.

Upvotes: 0

Views: 165

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

Just add it in manually:

select id, site_id, date from table1 union all
select id, null as site_id, date from table2 union all
select id, site_id, date from table3 union all
select id, site_id, date from table4;

Upvotes: 2

Related Questions