I have a feature that gets the record for specific id and has two links for the previous and next record. Right now I have separate queries to obtain the next id:
$query = "SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1";
$getId is the current id.
Is there a way to consolidate into one query?
SELECT A.id AS previd, B.id AS nextid
FROM
(SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 1) A,
(SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1) B
Returns 2 columns: previd
and nextid
surrounding $getId
i think you can use inner query
SELECT id FROM presentations
WHERE id >= ( SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 0 , 1 )
ORDER BY id ASC LIMIT 0 , 3
first array result will be previous record, second will be your current record, and the last will be your last record.