I am trying to concoct a mysql query that returns, in a single row, the fields for a given row, as well as a few fields from a row that matches the "previous" position, and the same fields for the "next" position. I'm pretty new at mysql, but for all the scouring the net for answers, this is the best I can do:
SELECT *,
(select id
from mytable t2
where t2.added_on < t.added_on
order by t2.added_on DESC
limit 1
) as prev_id,
(select id
from mytable t3
where t3.added_on > t.added_on
order by t3.added_on
limit 1
) as next_id FROM mytable as t ORDER BY `added_on`
which works, but only gives me the id
field for the "previous" and "next". As you may know, using *
(or 'id', 'title'
) instead of id
in the subqueries gives an error. I've looked into using JOIN
s and some other approaches but I'm just simply not getting it.