How to know prev id value and next id value from a table where id is given ?
like ia have id=5 for a recod in table then i want to know the prev id value and next id value
How to know prev id value and next id value from a table where id is given ?
like ia have id=5 for a recod in table then i want to know the prev id value and next id value
If id
is an auto-increment INT field,
SELECT * FROM tablename WHERE id < 5 ORDER by ID DESC LIMIT 0,1;
SELECT * FROM tablename WHERE id > 5 ORDER by ID ASC LIMIT 0,1;
should do the job.
Perhaps something like...
SELECT MAX(id) AS previous
FROM my_table
WHERE id < 5;
and
SELECT MIN(id) AS next
FROM my_table
WHERE id > 5;
would get what you want.