首页 > 代码库 > 使用SQL语句查询某记录的前后N条数据

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

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即可。

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