用union的时候怎么用orderby

由于使用union 的时候在每一个表中是不可以使用order by 的

比如:select a.id,a.name  from a order by a.id

    union 

  select b.id,b.name from b order by b.id

这样是错误的

需要将order by 移到union的外边去,即:

select *from

(select a.id,a.name  from a

    union 

  select b.id,b.name from b )as a order by id

 

posted on 2015-06-17 23:15  找到自己的灰姑娘  阅读(793)  评论(0)    收藏  举报