Solved

MySQL search, previous record

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

3 Use Cases for Connected Systems

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

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.…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now