谁来帮忙优化一下这条SQL语句

wangfei99168 2011-08-20 04:21:07
其实就是从视图中搜索第16条记录之后的15条记录。
使用的数据库为SQL 2000
求更优化的语句!!

SELECT * FROM V_view WHERE sortID =89 AND id IN (SELECT TOP 15 id FROM V_view WHERE sortID =89 AND id NOT IN(SELECT TOP 15 id FROM V_view WHERE sortID =89 ORDER BY id desc ) ORDER BY id desc ) ORDER BY id desc

谢谢大侠们!


...全文
120 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiangqing 2011-08-21
  • 打赏
  • 举报
回复
SELECT top 15 * FROM V_view WHERE sortID =89 AND id IN (select top 30 id FROM V_view WHERE sortID =89 order by id desc) order by id desc
Warren 2011-08-21
  • 打赏
  • 举报
回复
应该和“先降序取30条,在从这30条里升序去15条”等价吧?
;with DESC_top30
as
(
select top 30 *
from V_view
where sortID = 89
order by ID desc
)
select top 15 *
from DESC_top30
order by ID asc
我腫了 2011-08-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bbbbbben 的回复:]
取第十六条到第二十条
select top 30 * from tb
except
select top 15 * from tb
[/Quote]


又打错了 是 取第十六条到第三十条
我腫了 2011-08-21
  • 打赏
  • 举报
回复
取第十六条到第二十条
select top 30 * from tb
except
select top 15 * from tb
我腫了 2011-08-21
  • 打赏
  • 举报
回复
select top 31 * from tb
except
select top 16 * from tb
geniuswjt 2011-08-20
  • 打赏
  • 举报
回复

select top 15 * from tb
where id not in
(select top 16 id from tb order by id asc/*|desc*/)
order by id asc/*|desc*/
--小F-- 2011-08-20
  • 打赏
  • 举报
回复
取n到m行

1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)

2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc

3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc


4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename

取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true


5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
LMAOhuaNL 2011-08-20
  • 打赏
  • 举报
回复
select top 15 * from (
select top(16+15) * from tb order by id desc
) aa order by id desc

gw6328 2011-08-20
  • 打赏
  • 举报
回复

select top 15 * from (
select top(16+15) * from tb order by id desc
) aa order by id desc

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧