日期:2014-05-18  浏览次数:20527 次

对union all中的每个查询分别排序的问题
我现在有两个表,结构完全相同,我想要把两个表满足条件的数据都检索出来,并且检索出来的数据不要混在一起。先是表1的数据,再是表2的数据。另外,对这两个表检索的数据的排序条件也不一样,要分别排序。
我写成下面的形式,会有错误。
select   a,b,c   from   tablex   where   ...   order   by   a
union   all
select   a,b,c   from   tabley   where   ...   order   by   b,c
改成下面的形式,还是有错误
select   *   from(select   a,b,c   from   tablex   where   ...   order   by   a)m
union   all
select   *   from(select   a,b,c   from   tabley   where   ...   order   by   b,c)n

我使用的数据库是sql   server2005,不知道这个语句应该怎么写!
求求各位大侠帮忙啦!


------解决方案--------------------
try

select * from(select TOP 100 Percent a,b,c from tablex where ... order by a)m
union all
select * from(select TOP 100 Percent a,b,c from tabley where ... order by b,c)n