sql语句查询,某一记录上下相邻的两条记录。怎么写?
答案:4 悬赏:10 手机版
解决时间 2021-11-26 18:57
- 提问者网友:骨子里的高雅
- 2021-11-26 08:41
sql语句查询,某一记录上下相邻的两条记录。怎么写?
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-11-26 10:09
select * from news a
where news_id > 12345 and not exists(select 1 from news where news_id > 12345 and
news_id < a.id )
or
news_id < 12345 and not exists(select 1 from news where news_id < 12345 and
news_id > a.id )
如果不需要用一条语句写出来,那分开写的话,效率会好一些,这样写必须用一个or关键字
分开写:
select max(news_id) from news where news_id < 12345
select min(news_id) from news where news_id > 12345
where news_id > 12345 and not exists(select 1 from news where news_id > 12345 and
news_id < a.id )
or
news_id < 12345 and not exists(select 1 from news where news_id < 12345 and
news_id > a.id )
如果不需要用一条语句写出来,那分开写的话,效率会好一些,这样写必须用一个or关键字
分开写:
select max(news_id) from news where news_id < 12345
select min(news_id) from news where news_id > 12345
全部回答
- 1楼网友:山河有幸埋战骨
- 2021-11-26 11:59
排序规则是什么? 时间?ID?追问ID不是连续的,但是是从小到大的追答是这个意思?
select * from news where news_id=ID+1 or news_id=ID-1
select top 1 * from news where news_id in (select top 1 news_id from news where news_id union
select top 1 news_id from news where news_id>ID order by news_id )
order by news_id
select * from news where news_id=ID+1 or news_id=ID-1
select top 1 * from news where news_id in (select top 1 news_id from news where news_id
select top 1 news_id from news where news_id>ID order by news_id )
order by news_id
- 2楼网友:英雄的欲望
- 2021-11-26 11:41
news_id 是 int 吗?
是的话:
select * from news where news_id =12345+1 or news_id=12345-1
12345应该是传进来的值吧
select * from news where news_id = 变量+1 or news_id=变量-1追问news_id 不连续。。。追答select * from (select top 1 * from news where news_id<3 order by news_id desc) a
union all
select * from (select top 1 * from news where news_id>3 order by news_id)b
姐的效率高呀,啧啧
是的话:
select * from news where news_id =12345+1 or news_id=12345-1
12345应该是传进来的值吧
select * from news where news_id = 变量+1 or news_id=变量-1追问news_id 不连续。。。追答select * from (select top 1 * from news where news_id<3 order by news_id desc) a
union all
select * from (select top 1 * from news where news_id>3 order by news_id)b
姐的效率高呀,啧啧
- 3楼网友:北方的南先生
- 2021-11-26 10:37
简单的:
select Top 1 * from ID Where news_id>12345
select Top 1 * from ID Where news_id<12345追问这个能查到 12345后一条记录,和表的第一条记录追答后面加个排序规则就行了。自己拓展一下
select Top 1 * from ID Where news_id>12345
select Top 1 * from ID Where news_id<12345追问这个能查到 12345后一条记录,和表的第一条记录追答后面加个排序规则就行了。自己拓展一下
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯