MySQL查询上一条记录和下一条记录
2024-08-30 03:00:48
如果ID是主键或者有索引,可以直接查找:
方法一:
查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions];
查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions];
方法二:
查询上一条记录的SQL语句((如果有其他的查询条件记得加上other_conditions以免出现不必要的错误))
select * from table_a where id = (select max(id) from table_a where id < {$id} [and other_conditions]) [and other_conditions];
查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select * from table_a where id = (select min(id) from table_a where id > {$id} [and other_conditions]) [and other_conditions];
最新文章
- 【Android自学日记】搭建Android开发环境
- ROS机器人语音交互(一)
- Console API 与命令行
- Ubuntu14.04安装redis和简单配置
- SRS用列建模
- Linux 多线程应用中如何编写安全的信号处理函数
- java初级开发中的报错问题
- android下拉选择框spinner
- DevExpress GridControl 显示行号、设置行号宽
- Memcahce(MC)系列(一)Memcache介绍、使用、存储、算法、优化
- 开源项目:底部动作条(BottomSheet)
- python网络编程之单线程之间的并发
- 我的2017年终总结(PF项目框架设计心得分享 1.0rc new)
- MySQL表的创建
- hello1以及hello2的部分代码分析
- java之异常统一处理
- jq demo—图片翻页展示效果 animate()动画
- CH4401 蒲公英
- OpenGL中摄像机矩阵的计算原理
- 【JS】一款好用的JS日历选择插件【bootstrap-datetimepicker.js】
热门文章
- 《JAVA设计模式》之组合模式(Composite)
- [CF960G]Bandit Blues(第一类斯特林数+分治卷积)
- bjsxt学习笔记:Dubbo
- deque(双向队列)基本用法
- 13、numpy——算术函数
- Codeforces - 1195D1 - Submarine in the Rybinsk Sea (easy edition) - 水题
- spark连接hive找不到table
- C# DATETIME格式转换汇总 根据日期获取星期
- 20191114PHP文件操作
- C#实现百度ping功能