?
Solved

MySQL search, previous record

Posted on 2009-07-10
2
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
Joep_Killaars earned 500 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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