简单的一条查询语句

wangwei155 2006-08-07 10:02:51
我想直接用select 语句实现 查询出 已知条件下的 第31到第40条记录
select (怎么写) from T where (查询条件) order by ID
谢了
...全文
217 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mugua604 2006-08-07
  • 打赏
  • 举报
回复
wangwei155() ( ) 信誉:100 2006-8-7 11:10:11 得分: 0



谢谢大家,实验通过,语句如下:
select top 10 *
from T
where id not in (select top 30 id from T order by id asc)
order by id asc
结帖
这是在ID唯一的情况下才可用的!如果ID不唯一,将是错误的!
友情提醒!


wangwei155 2006-08-07
  • 打赏
  • 举报
回复
谢谢大家,实验通过,语句如下:
select top 10 *
from T
where id not in (select top 30 id from T order by id asc)
order by id asc
结帖
christianye 2006-08-07
  • 打赏
  • 举报
回复
select top 11 * from tb
where id not in(select id from
(select top 29 id from tb ) x
)
order by id

这道面试题上次有人问过了。。。
wangwei155 2006-08-07
  • 打赏
  • 举报
回复
谢了大家
wdjlily(暗黑使者),你的肯定不符合,我有查询条件ID不一定是连续的,只是按照它排序而已


playwarcraft 2006-08-07
  • 打赏
  • 举报
回复
set rowcount 40
select [tmp]=identity(int,1,1),* into #t from T where (查询条件) order by ID
set rowcount 0
select [id],XXX from #t where [tmp] between 31 and 40
wdjlily 2006-08-07
  • 打赏
  • 举报
回复
select *
from table
where (查询条件) and ID between 31 and 40
order by ID
九斤半 2006-08-07
  • 打赏
  • 举报
回复
假设ID是主键
查询出 已知条件下的 第31到第40条记录

--select (怎么写) from T where (查询条件) order by ID
select top 10 *
from test
where id not in (select top 30 id from test order by num)
order by id
lzuyh 2006-08-07
  • 打赏
  • 举报
回复
学习
Yang_ 2006-08-07
  • 打赏
  • 举报
回复
这不是好方法,因为最后的排序反了,效率也不好

不过sqlserver没有更好方法,用临时表可能效率更差
Yang_ 2006-08-07
  • 打赏
  • 举报
回复
select top 10 * from (
select top 40 * from T where (查询条件) order by ID
) as t
order by id desc

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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