UNION ALL query: “Too Many Fields Defined”

后端 未结 3 926
南方客
南方客 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: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]
    

提交回复
热议问题