example:
select a,b,c from
(select aaa a,bbb b,ccc c from table_1 where....
union 
select eee a,fff b,ggg c from table_2 where....
union
...)
order by a

解决方案 »

  1.   

    也可以这样啊:
    select 1 as 序号,bbb b,ccc c from table_1 where....
    union 
    select 2 as 序号,fff b,ggg c from table_2 where....
    union
    ..........效率不会降低的.
      

  2.   

    union本来就是根据select中的字段顺序group by的,如果用
    select a,b,c from
    (select aaa a,bbb b,ccc c from table_1 where....
    union 
    select eee a,fff b,ggg c from table_2 where....
    union
    ...)
    order by a;
    那其实是进行了两次排序,第一次是对union进行group by,第二次是根据order进行排序。建议
    1、用union all取代union,效率高很多
    2、或者在select语句中根据order的顺序组织,就不需要进行第二次排序了。