Solved

MySQL search, previous record

Posted on 2009-07-10
2
207 Views
Last Modified: 2012-05-07
Hi
I am creating a shop and my client has just asked if they can have a next and previous button when viewing a product. These two links would also feature the names of the next and previous products.
When the products are listed they are listed on the page in the order of the primary key descending.
So when they are viewing a product with a particular id what is the easiet way to find the next a previous product in the databse and display their details (name).
Many thanks
Matt
0
Comment
Question by:Bigshowmg
2 Comments
 
LVL 3

Accepted Solution

by:
Joep_Killaars earned 125 total points
ID: 24822066
Well, asuming you have the current id on your page a query sorting the keys would do the trick.
// find the next entry.
// sql server
select top 1 * from products where key > @curKey order by key asc
// mysql
select * from products where key > @curKey order by key asc LIMIT 1
 
// find the previous entry.
// sql server
select top 1 * from products where key < @curKey order by key desc
//mysql
select * from products where key < @curKey order by key desc LIMIT 1

Open in new window

0
 

Author Comment

by:Bigshowmg
ID: 24822330
Great stuff, thank you
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 49
Connection to multiple databases 13 26
SQL Syntax 14 35
Reformat SQL - so SSRS can read the columns 25 14
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question