Oracle: Combine Two Tables with Different Columns

杀马特。学长 韩版系。学妹 提交于 2019-12-12 22:11:08

问题


This is table 1:

col_1  col_2  date_1
-----  -----  ------
1      3      2016
2      4      2015

And this is table 2:

col_3  col_4  date_2
-----  -----  ------
5      8      2014
6      9      2012

I want a result like this:

col_1  col_2  col_3  col_4  date_1  date_2
-----  -----  -----  -----  ------  ------
1      3      NULL   NULL   2016    NULL
2      4      NULL   NULL   2015    NULL
NULL   NULL   5      8      NULL    2014
NULL   NULL   6      9      NULL    2012

Any solutions?


回答1:


Using Union All and Null as a different column:

SELECT col_1, col_2, NULL as col_3, NULL as col_4,
       date_1, NULL as date_2
FROM table_1

Union All

SELECT NULL, NULL, col_3, col_4, NULL, date_2
FROM table_2



回答2:


Use union all:

select col_1, col_2, NULL as col_3, NULL as col_4, date_1, NULL as date_2
from table1
union all
select NULL, NULL, col_3, col_4, NULL, date_2
from table2;



回答3:


Using Join:

select t1.col_1,t1.col_2,t2.col_3,t2.col_4,t1.date_1,t2.date_2
from t1
full join t2
on t1.col_1=t2.col_3
order by t1.col_1;


来源:https://stackoverflow.com/questions/40378914/oracle-combine-two-tables-with-different-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!