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

表searchId为当前id

1.查询上一条数据

select * from tbContent where id=(select max(id) from tbContent where id

2.查询下一条数据

select * from tbContent  where id=(select min(id) from tbContent  where id>searchId)

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

select * from tbContent where 
id in((select max(id) from tbContent where id< searchId), 
(select min(id) from tbContent where id> searchId)) 

你可能感兴趣的:(SQL)