union关键字实现寻找当前记录的上一条和下一条

项目中要用到union关键字实现寻找当前记录的上一条和下一条。

首先:

select top 1 ID,title from Z_Article where [ID]<5 order by id desc union all select top 1 ID,title from Z_Article where ID>5

好像使用了union关键字就不支持内部排序了,报错。服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'union' 附近有语法错误。

 

于是使用下面的方法解决问题:

select * from (select top 1 ID,title from Z_Article where [ID]<5 order by id desc) as tb1
union
select * from (select top 1 ID,title from Z_Article where ID>5)as tb2


posted @ 2008-11-18 10:53  supers  阅读(301)  评论(0)    收藏  举报