使用从第二个字段开始的表2的数据来联合两个表

时间:2013-03-06 09:29:01

标签: sql ms-access

我有两个表,每个表有72列。第一个表的数据从column1开始,第二个表的数据从第30列开始。如何在两个表上执行Union,并在第二个表值的前29列中获取0?

1 个答案:

答案 0 :(得分:2)

为此,您必须显式写入29列,默认值为0(零)。

同样,您必须为表A中的列选择表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