您的位置:首页 > 博客中心 > 数据库 >

使用SQL语句查询某记录的前后N条数据

时间:2022-03-13 23:40

id是指当前数据tb_id参数

方法一:

string preSql = "select top 1 * from table where tb_id < " + id + " order by tb_id DESC"

string nextSql = "select top 1 * from table where tb_id > " + id + " order by tb_id ASC"


方法二:

string preSql = "select * from [table] where tb_id = (select MAX(tb_id) from [table] where tb_id<"+ id + ")";
string nextSql = "select * from [table] where tb_id = (select MIN(tb_id) from [table] where tb_id>"+ id + ")";

将查询结果union即可。

热门排行

今日推荐

热门手游