智睿软件_技术交流论坛综合站长Web程序开发 → SQL查询根据ID取前后记录ID号


  共有19708人关注过本帖树形打印

主题:SQL查询根据ID取前后记录ID号

帅哥哟,离线,有人找我吗?
zhirui
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:520 积分:8740 威望:0 精华:5 注册:2009-07-07
SQL查询根据ID取前后记录ID号  发帖心情 Post By:2009-10-15 07:10:29

我写得MAYBE有点烦,效率上不高,需要两个Select语句
假设取Table1表中记录号为5的前后记录的ID值。其中ID为自增长。
select top 1 id from table1 where id<5 order by id desc
select top 1 id from table1 where id>5 order by id
也可以在Table表中增加一个连续增长的字段,第N条记录值为N。这样索取也比较方便。
如果需要用一条语句来做的话,可以这样
select * from(
select top 1 id from table1 where id<5 order by id desc
union
select top 1 id from table1 where id>5 order by id ) as temp
select top(1) * from mytable where ID>"&ID
'不管你中间空多少 反正是查出你当前id的下一条
select top(1) * from WEBNEWS where ID<"&ID&"order by id desc"
'desc 表示降序排列,查上一条

企业开源网站管理系统,中小型企业的成长伙伴 支持(0中立(0反对(0回到顶部