我有两个表,每个表有 72 列。第一个表的数据从第 1 列开始,第二个表的数据从第 30 列开始。如何在两个表上进行联合并在第二个表值的前 29 列中获取 0?
问问题
79 次
1 回答
2
为此,您必须明确写入 29 列,默认值为 0(零)。
对于表 B 中存在的列的表 A 选择,您也必须这样做。
假设表 B 有 20 列名为 col30,col31,col32 ....... col49
您可以使用以下查询 -
select TableA. col1,
TableA. col2,
TableA. col3,
TableA. col4,
TableA. col5,
TableA. col6,
TableA. col7,
TableA. col8,
TableA. col9,
TableA. col10,
TableA. col11,
TableA. col12,
TableA. col13,
TableA. col14,
TableA. col15,
TableA. col16,
TableA. col17,
TableA. col18,
TableA. col19,
TableA. col20,
TableA. col21,
TableA. col22,
TableA. col23,
TableA. col24,
TableA. col25,
TableA. col26,
TableA. col27,
TableA. col28,
TableA. col29,
0 col30,
0 col31,
0 col32,
0 col33,
0 col34,
0 col35,
0 col36,
0 col37,
0 col38,
0 col39,
0 col40,
0 col41,
0 col42,
0 col43,
0 col44,
0 col45,
0 col46,
0 col47,
0 col48,
0 col49,
0 col50
from tableA
union all
select 0 col1,
0 col2,
0 col3,
0 col4,
0 col5,
0 col6,
0 col7,
0 col8,
0 col9,
0 col10,
0 col11,
0 col12,
0 col13,
0 col14,
0 col15,
0 col16,
0 col17,
0 col18,
0 col19,
0 col20,
0 col21,
0 col22,
0 col23,
0 col24,
0 col25,
0 col26,
0 col27,
0 col28,
0 col29,
TableB. col30,
TableB. col31,
TableB. col32,
TableB. col33,
TableB. col34,
TableB. col35,
TableB. col36,
TableB. col37,
TableB. col38,
TableB. col39,
TableB. col40,
TableB. col41,
TableB. col42,
TableB. col43,
TableB. col44,
TableB. col45,
TableB. col46,
TableB. col47,
TableB. col48,
TableB. col49,
TableB. col50
from TableB
于 2013-03-06T09:42:34.183 回答