[英]Union with different number of columns
我在第一個表中有三列,第二個表有一列存在於第一個表中,2列我想要添加。
例如:
select c1 as col1, c2 as col2, c3 as col3
from Table1
union
select c1, c4 as col4, c5 as col5
from Table2
expected Result:
col1,col2,col3,col4,col5
只需添加null
或您喜歡的任何其他默認值作為靜態列
select c1 as col1,
c2 as col2,
c3 as col3,
null as col4,
null as col5
from Table1
union
select c1,
null,
null,
c4,
c5
from Table2
您已經問過這個問題,並得到了答案 - https://stackoverflow.com/questions/18923218/unioning-tables-with-different-number-of-columns/18923250#18923250 。 是否有可能實際上你需要一個聯合 ,而不是聯盟:
select
t1.c1 as col1,
t1.c2 as col2,
t1.c3 as col3,
t2.c4 as col4,
t2.c5 as col5
from Table1 as t1
inner join Table2 as t2 on t2.col1 = t1.col1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.