Combining UNION ALL and ORDER BY in Firebird

前端 未结 6 2176
梦谈多话
梦谈多话 2020-12-06 10:36

This is my first attempt at answering my own question, since someone may well run into this and so it might be of help. Using Firebird, I want to combine the results of two

相关标签:
6条回答
  • 2020-12-06 10:56

    How about:

    select C1, C2, C3 from T1
    union all 
    select C1, C2, C3 from T2
    order by 2
    

    At least in the newer Firebird Versions it works if you order by "Number" instead of using an Alias.

    0 讨论(0)
  • 2020-12-06 10:59

    In Firebird 1.5 this works for me

    create view V1 (C1, C2, C3) as
      select C1, C2, C3 from T1
      union all 
      select C1, C2, C3 from T2
    

    and then

    select C1, C2, C3 from V1 order by C3
    
    0 讨论(0)
  • 2020-12-06 11:00

    Field names are not required to be equal. That's why you can't use the field name in the order by.
    You may use the field index instead. As in:

    (select C1, C2, C3 from T1)
    union all 
    (select C7, C8, C9 from T2)
    order by 3  
    
    0 讨论(0)
  • 2020-12-06 11:03

    Perform the UNION ALL in a view (without the ORDER BY clause), then select from the view using ORDER BY.

    0 讨论(0)
  • 2020-12-06 11:11

    Moving order by into a query tail has no effect to output datagrid.

    select * from (
        select first 1
            C1
        from T1
        order by id desc
    )
    union all
    select * from (
        select first 1
            C1
        from T2
        order by id desc
    )
    0 讨论(0)
  • 2020-12-06 11:13
    SELECT C1, C2, C3
    FROM (
        select C1, C2, C3 from T1
        union all 
        select C1, C2, C3 from T2
    )
    order by C3
    
    0 讨论(0)
提交回复
热议问题