Solved

MySQL search, previous record

Posted on 2009-07-10
2
204 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
'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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

19 Experts available now in Live!

Get 1:1 Help Now