在不排序情况下
select top 5 * from table where nid=1
union all
select top 10 * from table where nid=2
 这个sql语句能运行
select top 5 * from table where nid=1 order by id desc
union all
select top 10 * from table where nid=2 order by id desc
排序就不能正确运行求 怎么改成在排序下也能运行

解决方案 »

  1.   

    select * from (select top 5 * from ..........)
    union all
    select * from (select top 10 * from ............)
      

  2.   

    select * from (
    select top 5 * from table where nid=1
    union all
    select top 10 * from table where nid=2)a
    order by id desc
      

  3.   

    union/union all只能是最后一个查询用排序,并且排序的字段必要是第1个查询中存在的字段
    即:
    select top 5 * from table where nid=1
    union all
    select top 10 * from table where nid=2 order by id desc像下面这样是不对的
    select A,B from t1
    union all
    select A,D from t2 order by A,D