Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find previous/next records when given alphabetic order in sql?

Tags:

sql

mysql

I have a table people

id    name
----+-----
54    Tango
76    Alpha
95    Radio

When displaying the records on the web those names are ordered alphabetically

Alpha, Radio, Tango

Lets say we're viewing Radio details and want to navigate to the next (Tango) or prevoius (Alpha) record using some links.

How can I find those id's to create the needed links?

Finding next superior or inferior id won't work.

Is this possible?

Thanks.

like image 505
gustyaquino Avatar asked Nov 30 '12 18:11

gustyaquino


1 Answers

it is not done so yet.

prev

SELECT Id FROM people WHERE name < 'Radio' ORDER BY name DESC LIMIT 1

next

SELECT Id FROM people WHERE name > 'Radio' ORDER BY name ASC LIMIT 1
like image 124
Hamlet Hakobyan Avatar answered Oct 09 '22 14:10

Hamlet Hakobyan