[Solved] select 2 previous and 2 next records with one in between [closed]


If your records are ordered by ID, you could use a query like this:

(SELECT *
FROM yourtable
WHERE ID<5 AND catID=(SELECT catID FROM yourtable WHERE ID=5)
ORDER BY ID DESC
LIMIT 2)
UNION ALL
(SELECT *
FROM yourtable
WHERE ID>5 AND catID=(SELECT catID FROM yourtable WHERE ID=5)
ORDER BY ID
LIMIT 2)

Please see fiddle here.

3

solved select 2 previous and 2 next records with one in between [closed]