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

以下为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关键字即可。

posted @ 2020-06-10 17:45  农名工进城  阅读(940)  评论(0编辑  收藏  举报