在不排序情况下
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
排序就不能正确运行
求 怎么改成在排序下也能运行
------解决方案--------------------------------------------------------
select * from (select top 5 * from ..........)
union all
select * from (select top 10 * from ............)
------解决方案--------------------------------------------------------
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
------解决方案--------------------------------------------------------
不过你最好首先解决“怎样让其不排序”的问题,实在不能解决,明白了白白浪费时间去排序的风险责任,再这样写。
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
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
------解决方案--------------------------------------------------------
实在不行 存储过程一下。