Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select 'previous' and 'next' records in 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. alt text

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.

alt text

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.

like image 741
jessegavin Avatar asked Dec 08 '22 03:12

jessegavin


1 Answers

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.

like image 87
TomTom Avatar answered Dec 23 '22 16:12

TomTom