sql语句查询,某一记录上下相邻的两条记录。怎么写?
发布网友
发布时间:2022-04-12 14:10
我来回答
共4个回答
热心网友
时间:2022-04-12 15:39
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
热心网友
时间:2022-04-12 16:57
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_id3 order by news_id)b
姐的效率高呀,啧啧
热心网友
时间:2022-04-12 18:32
简单的:
select Top 1 * from ID Where news_id>12345
select Top 1 * from ID Where news_id<12345追问这个能查到 12345后一条记录,和表的第一条记录
追答后面加个排序规则就行了。自己拓展一下
热心网友
时间:2022-04-12 20:23
排序规则是什么? 时间?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_idID order by news_id )
order by news_id