sql语句查询,某一记录上下相邻的两条记录。怎么写?

2024-12-21 08:07:54
推荐回答(4个)
回答1:

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

回答2:

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

回答3:

简单的:
select Top 1 * from ID Where news_id>12345

select Top 1 * from ID Where news_id<12345

回答4:

排序规则是什么? 时间?ID?