到底有没有这样的语句 请高手赐教

wind513 2007-01-05 10:42:58
假如我现在有一个id 我想查询和id邻近的几个id 前后都可以
...全文
227 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
十一月猪 2007-01-09
  • 打赏
  • 举报
回复
呵呵 方法全了
L5160729 2007-01-07
  • 打赏
  • 举报
回复
学习
rookie_one 2007-01-06
  • 打赏
  • 举报
回复
给点就行了,快揭帖把,呵呵
wind513 2007-01-05
  • 打赏
  • 举报
回复
多谢大家帮忙, 分少了, 给不过来啊!
rookie_one 2007-01-05
  • 打赏
  • 举报
回复
create table #temp
(id varchar(50)
)
insert into #temp
select '1' union all select '2' union all select '3' union all select '4' union all select '5' union all select '6' union all select '7' union all select '8'
select * from #temp
----------------------
id
1
2
3
4
5
6
7
8


查询前后两位ID
select * from #temp
where
[id] in(select distinct top 2 [id] from #temp where [id]<5 order by [id] DESC)
or
[id]=5
or
[id] in(select distinct top 2 [id] from #temp where [id]>5 order by [id] ASC)
-------------
id
3
4
5
6
7
rookie_one 2007-01-05
  • 打赏
  • 举报
回复
select * from #temp
where
[id] in(select distinct top 范围数 [id] from #temp where [id]<参照id order by [id] DESC)
or
[id]=参照id
or
[id] in(select distinct top 范围数 [id] from #temp where [id]>参照id order by [id] ASC)
rookie_one 2007-01-05
  • 打赏
  • 举报
回复
select * from tablename
where
id in(select distinct top 范围数 [id] from tablename where id<参照id order by id DESC)
or
id in(select distinct top 范围数 [id] from tablename where id>参照id order by id ASC)
marco08 2007-01-05
  • 打赏
  • 举报
回复
--try


select top N * from tbName where id<'' order by id desc
union all
select * from tbName where id=''
union all
select top N * from tbName where id>'' order by id
leo_lesley 2007-01-05
  • 打赏
  • 举报
回复
select top 个数 * from tablename where id<查询的id order by id desc
select top 个数 * from tablename where id>查询的id order by id
caixia615 2007-01-05
  • 打赏
  • 举报
回复
select top 2 * from tablename where id<指定id order by id desc
select top 2 * from tablenam where id>指定id order by id

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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