I'm trying to build a chained list of items, in which the first one has a parent_id = 0, while the other ones should be sorted by what the last element's id was related to its parent_id.
This is the expected result:
| id | parent_id | |----|-----------| | 9 | 0 | | 2 | 9 | | 3 | 2 | | 14 | 3 | | 5 | 14 |
How can I do this kind of ORDER BY clause? Currently I'm at
parent_id = 0 desc, parent_id desc
But the output of this is wrong, of course, because it doesn't depend on the last ID, it's just running a desc ordering of parent_id:
| id | parent_id | |----|-----------| | 9 | 0 | | 5 | 14 | | 2 | 9 | | 14 | 3 | | 3 | 2 |
Thanks in advance.