查询某条数据的上一条和下一条数据的Sql语句

searchNum:为当前查询的编号

1.查询上一条数据

select * from tbl_stu where num = (select max(num) from tbl_stu where num < searchNum)

2.查询下一条数据

select * from tbl_stu where num = (select min(num) from tbl_stu where num > searchNum)

3.查询上一条和下一条数据

select * from tbl_stu 
where num in(
  (select max(num) from tbl_stu where num < searchNum), 
  (select min(num) from tbl_stu where num > searchNum)
)
posted @ 2021-07-06 10:51  码农小匠  阅读(1478)  评论(0编辑  收藏  举报