2011-1118

SQL查询当前数据上一条和下一条的记录

作者: momy 分类: 数据库 0 Comment »
摘要:SQL查询当前数据上一条和下一条的记录
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 + ")";
标签: SQL 阅读: 11940
上一篇: UML中的关系 - 10251次
下一篇: 几种常用数据库查询前n条记录的方法 - 11718次

向右滑动解锁留言