I have a mysql table structured like this (removed other columns, not relevant to the question):
id parent_id
----------- -----------
1 0
2 0
3 0
4 3
5 2
6 1
7 1
A "parent" is a row that has parent_id = 0
.
I would like to know if there is a way to formulate the query so that MySQL outputs the rows with each children right after its parents?
The intended result would be
id parent_id
----------- -----------
1 0
6 1
7 1
2 0
5 2
3 0
4 3
Thank you for your time.