MySQL实现根据当前ID读取上一条和下一条记录

时间:2021-10-28 09:48:02

以下为MySQL语句演示:

SELECT * FROM t_news AS n ORDER BY n.`News_ID`

当前ID为4,读取上一条记录:

    SELECT * FROM t_news AS n WHERE n.`News_ID` < 4 ORDER BY n.`News_ID` DESC  LIMIT 0,1;  

当前ID为4,读取下一条记录:

    SELECT * FROM t_news AS n WHERE n.`News_ID` > 4 ORDER BY n.`News_ID` LIMIT 0,1;  

其他数据库,不用limit,改用top关键字即可。