I want to ask on how to get the next and previous data when I select the "8787"
This is the sample data on table Name:
NameId is not auto number.
NameID RName
6767 Apple
8787 Hallow
8627 Orange
Thanks
I want to ask on how to get the next and previous data when I select the "8787"
This is the sample data on table Name:
NameId is not auto number.
NameID RName
6767 Apple
8787 Hallow
8627 Orange
Thanks
To get the next ID, throw away all IDs that are not larger, sort the table by the ID so that the next ID is the first one, then take only this one record:
SELECT *
FROM MyTable
WHERE NameID > 8787
ORDER BY NameID
LIMIT 1
Similar for the previous ID:
SELECT *
FROM MyTable
WHERE NameID < 8787
ORDER BY NameID DESC
LIMIT 1
Based on CL.'s answer, if you want it all in one query:
select a.*,
(select RName
from t1 b
where b.NameID < a.NameID
order by NameID desc limit 1) as prev_rname,
(select NameID
from t1 b
where b.NameID < a.NameID
order by NameID desc limit 1) as prev_NameID,
(select RName
from t1 c
where c.NameID > a.NameID
order by NameID asc limit 1) as next_rname,
(select NameID
from t1 d
where d.NameID > a.NameID
order by NameID asc limit 1) as next_NameID
from t1 a
order by a.NameID;