Mysql – How to select the next available row id of varchar type in MySQL

MySQLPHP

I have a table with primary key 'Id' of varchar type. I want to select the next row when user clicks next record, in php, for example
1st row id= 'a'
2nd row id= 'b'
3rd row id='h'
4th row id='z'

Im currently on page with get variable id='a'
I want to get the next which is 'b', then 'h' then 'z'

The first record is easy as I send page.php?id='a' to display
table records.

The next record,
Select id from table where id > 'a'
But I dont want 'h' or 'z' because 'b' is next.
I dont have a clue, any help will be appreciated

Best Answer

SELECT id AS next_id FROM your_table WHERE id > @current_id ORDER BY id LIMIT 1

Breaking that down:

SELECT id AS next_id FROM your_table

that bit is hopefully obvious

WHERE id > @current_id 

you don't want the current value or anything before it

ORDER BY id 

in the output, list earlier ID values first

LIMIT 1

we only want one of them so no point going further once one has been ready from the table.

As an aside: is there a any particular reason why you are using a character field as the ID instead of a numeric one which would be more efficient?