This question is wrong. I had some very big misunderstanding about how union works. I am reading about it now.
edit 04.12.2016 If you are still intersted, you can go here Selecting the right column
I have something like this
with table3 as
(
select t1.c1, t1.c2...
from table1 t1
union all
select t2.c1, t2.c2...
from table2 t2
)select * from table3
I need to insert all rows from above in another table
insert into table4 t4
(
t4.c1, t4.c2...
)
select t3.c1, t3.c2...
from table3 t3
My question is, will this insert work. I have clumns in table 1 and 2 named the same, will I need to reference them somehow differently?
Do I need to write it like this?
insert into table4 t4
(
t4.c1, t4.c2...
)
select t3.t1.c1, t3.t1.c2, t3.t2.c1...
from table3 t3
with
is part ofselect
statement. You caninsert
result ofselect
and you can usewith
in thisselect
. Maybe syntax is not the most intuitive but this should work:And no you don't need (even can't) use double aliases.
No alias needed
if the column match you could simply use insert select
otherwise you should declare the column name
Assuming that you needto use that
UNION ALL
, instead of single insert-as-select statements to insert into another table, you can try to use different aliases for columns from different tables: