Solved

MySQL search, previous record

Posted on 2009-07-10
2
209 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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