Suppose I have a table 'prlines' with a self relation on adjacent records:
id | prev_id
------------
1 | NULL
2 | NULL
3 | 1
4 | 2
5 | 3
6 | 5
I want to get all connected IDs (previous/next) of a certain record. For example:
SELECT `prev_id` FROM `prlines` ... WHERE id = 5;
Should produce this output:
prev_id
-------
3
1
6
What I am doing currently is making a while loop in python that generates multiple queries to follow the relationship for each record. Any ideas to achieve this in a single mysql query?