日期:2014-05-19  浏览次数:20605 次

一个sql嵌套语句,请大侠帮忙分析一下
select   *   from   table_a   where   a_id   in  
(select   b_id   from   (select   top   10   b_1   from  
(select   top   100   b_id   from   table_b   order   by   b_1   asc)   aa
order   by   b_1   desc)   aa
order   by   b_1   asc)

该语句执行师出错,提示如下:
消息   1033,级别   15,状态   1,第   5   行
除非另外还指定了   TOP   或   FOR   XML,否则,ORDER   BY   子句在视图、内联函数、派生表、子查询和公用表表达式中无效。

请大侠帮忙分析一下,该如何修改
(该语句要实现的是:先从表table_b中选10条记录(b_id),按b_1升序第91到100条,然后从table_a中选10条记录,使a_id等于前面table_b中的10个b_id,并按照b_1的升序列出)

------解决方案--------------------
select * from table_a where a_id in
(select top 100 percent b_id from (select top 10 b_1 from
(select top 100 b_id from table_b order by b_1 asc) aa
order by b_1 desc) aa
order by b_1 asc)
------解决方案--------------------
--如果子查询要排序,需要加top ...

select * from table_a where a_id in
(select top 100 percent b_id from (select top 10 b_1 from
(select top 100 b_id from table_b order by b_1 asc) aa
order by b_1 desc) aa
order by b_1 asc)