以文本方式查看主题

-  智睿软件_技术交流论坛  (http://zhirui.net/bbs/index.asp)
--  Web程序开发  (http://zhirui.net/bbs/list.asp?boardid=22)
----  SQL查询根据ID取前后记录ID号  (http://zhirui.net/bbs/dispbbs.asp?boardid=22&id=53)

--  作者:zhirui
--  发布时间:2009-10-15 07:10:29
--  SQL查询根据ID取前后记录ID号
我写得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 表示降序排列,查上一条