如何在SQL中选择“上一个"和“下一个"记录? [英] How to select 'previous' and 'next' records in SQL?

查看:112
本文介绍了如何在SQL中选择“上一个"和“下一个"记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的网站上构建博客文章详细信息页面,该页面将显示显示"上一个"和"下一个"链接,类似于打字机博客文章的工作方式.请参见下面的示例.

I am building a blog-post detail page on my site that will display display a 'previous' and 'next' link similar to how a typepad blog post works. See example below.

我想知道是否有一种查询数据库的方法,该方法可以完成如下图所示的操作:选择当前"记录(黄色)进行显示,还选择下一个和上一个记录(绿色)按PublishDate排序.

I am wondering if there is a way to query the database which accomplishes something like the image below where it selects the 'current' record (in yellow) for display, but also selects the next and previous records (in green) when sorted by PublishDate.

为澄清起见,我正在使用的数据库表具有唯一的发布日期.抱歉,示例图像显示了多个具有相同PublishDate的记录.

Also, for clarification, the database table I am working with has unique publish dates. Sorry that the example image shows multiple records with the same PublishDate.

推荐答案

您没有. SQL没有上一个,下一个概念.基本上,您会在已查询的排序查询(例如日期)中查询下一个前1个查询.这意味着您需要(按语句逐个命令)过滤订单.

You dont. SQL has no previous, next concept. YOu basically ask for the next top 1 in an ordered query (by date for example) after the one you already have. THis means you need to have / impost an order (by order by statement) for filtering.

这篇关于如何在SQL中选择“上一个"和“下一个"记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆