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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。