UNION ALL query: “Too Many Fields Defined”

后端 未结 3 899
南方客
南方客 2021-01-12 22:57

I\'m trying to get a UNION of 3 tables, each of which have 97 fields. I\'ve tried the following:

select * from table1
union all
select * from table2
union a         


        
相关标签:
3条回答
  • 2021-01-12 23:27

    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.

    0 讨论(0)
  • 2021-01-12 23:31

    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;
    
    0 讨论(0)
  • 2021-01-12 23:41

    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]
    
    0 讨论(0)
提交回复
热议问题