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


浙公网安备 33010602011771号