星五博客

按日期排序获取上下篇文章

declare @d datetime,@id int
set @id=5

set @d=(select fmeta from tnotes where fid=@id)
--上一篇
select top 1 fid,ftitle,fmeta
 from (select top 1 fid,ftitle,fmeta
	 from tnotes where fmeta>@d order by fmeta)t
--当前篇
select top 1 fid,ftitle,fmeta from tnotes where fid=@id
--下一篇
select top 1 fid,ftitle,fmeta
 from (select top 1 fid,ftitle,fmeta
	 from tnotes where fmeta<@d order by fmeta desc)t