id是指当前数据news_id参数
方法一:
string preSql = "select top 1 * from news where news_id < " + id + " order by news_id DESC"
string nextSql = "select top 1 * from news where news_id > " + id + " order by news_id ASC"
方法二:
string preSql = "select * from [news] where news_id = (select MAX(news_id) from [news] where news_id<"+ id + ")";
string nextSql = "select * from [news] where news_id = (select MIN(news_id) from [news] where news_id>"+ id + ")";
转载之(http://ruyu108.blog.163.com/blog/static/101231082010101992117165/)
本文介绍了两种使用SQL查询指定新闻ID前后记录的方法。方法一利用简单条件筛选并结合ORDER BY进行排序来获取紧邻目标ID的新闻记录;方法二则通过子查询找到最大或最小的相邻ID实现相同目的。
2247

被折叠的 条评论
为什么被折叠?



