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

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

上一篇:bdb insert测试


下一篇:将数据库函数添加到EntityFarmeWork模型中.md