最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SQL查询当前数据上一条和下一条的记录
时间:2022-06-29 08:08:49 编辑:袖梨 来源:一聚教程网
mssqlserver查询方法
其实我做了最简的就是如下写法
| 代码如下 | 复制代码 |
|
上一条记录的SQL语句: select top 1 * from news where newsid 下一条记录的SQL语句: select top 1 * from news where newsid>id order by newsid ASC |
|
另一种写未能
id是指当前数据news_id参数
方法一:
| 代码如下 | 复制代码 |
|
string nextSql = "select top 1 * from news where news_id > " + id + " order by news_id ASC" |
|
方法二:
| 代码如下 | 复制代码 |
|
|
|
mysql查询上一条与下一条的办法
如果ID是主键或者有索引,可以直接查找:
方法1:
| 代码如下 | 复制代码 |
| 1.select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1); 2.select * from table_a where id = (select id from table_a where id > {$id} order by id asc limit 1); |
|
方法2:
| 代码如下 | 复制代码 |
| 1.select * from table_a where id = (select max(id) from table_a where id < {$id}); 2.select * from table_a where id = (select min(id) from table_a where id > {$id}); |
|
其他更优的暂时还没想出来
相关文章
- Claude Design避坑指南:3个新手常犯的错误 05-27
- Project Genie 地区限制避坑:这些热门国家无法使用 05-27
- 戴文军个人简历怎么查?从打工仔到百亿掌门只用6年 05-27
- 绝味鸭脖戴文军简介:揭秘鸭王背后不为人知的创业真相 05-27
- 亲测京东JoyInside一个月,这些优缺点你必须知道 05-27
- uc浏览器网页版入口官网-手机/电脑uc浏览器网页版入口地址 05-26