7

我正在尝试获得 3 个表的 UNION,每个表都有 97 个字段。我尝试了以下方法:

select * from table1
union all
select * from table2
union all
select * from table3

这给了我一条错误消息:

Too many fields defined.

我还尝试从第一个表中显式选择所有字段名称(为简洁起见添加了省略号):

select [field1],[field2]...[field97] from table1
union all
select * from table2
union all
select * from table3

当我只 UNION 两个这样的表时,它工作正常:

select * from table1
union all
select * from table2

作为这个查询的结果,我不应该得到超过 97 个字段;两桌 UNION 只有 97。那为什么我要Too many fields3 桌呢?

编辑:正如 RichardTheKiwi 在下面指出的,Access 正在总结 UNION 链中每个 SELECT 查询的字段计数,这意味着我的 3 个表超过了 255 个字段的最大值。因此,我需要像这样编写查询:

select * from table1
union all
select * from
(select * from table2
union all
select * from table3)

效果很好。

4

3 回答 3

12

It appears that the number of fields being tracked (limit 255) is counted against ALL parts of the UNION ALL. So 3 x 97 = 291, which is in excess. You could probably create a query as a UNION all of 2 parts, then another query with that and the 3rd part.

于 2012-12-12T19:06:52.613 回答
1

I had two tables with 173 fields each (2 x 173 > 255!). So I had to resort to splitting the tables in half (keeping the primary key in both), before using the UNION statement and reassembling the resulting output tables using a JOIN.

    select u1.*, u2.* 
    from (
      select [field1_PKID],[field2],...,[field110] 
      from table1

      union all

      select [field1_PKID],[field2],...,[field110] 
      from table2
      ) as u1
    inner join (
      select [field1_PKID],[field111],...,[field173] 
      from table1

      union all 

      select [field1_PKID],[field111],...,[field173] 
      from table2
      ) as u2
    on [u1].[field1_PKID] = [u2].[field2_PKID]
于 2015-05-07T22:27:35.770 回答
0

Perhaps if your 3 tables have duplicate records you can go with UNION instead of UNION ALL which may reduce the number of fields to be tracked. Because UNION will always serve the business purpose which removes duplicates. In that case your query will be like following,

select * from table1
union
select * from table2
union
select * from table3;
于 2012-12-12T19:19:24.433 回答