id parent_id
1 0
2 0
3 2
4 0
5 1
6 0
I need a query that will return parent rows (parent_id=0) followed by its child rows:
- first parent
- all children of first parent
- second parent
- all children of second parent
- third parent
- fourth parent
Expected result: ordered by id
id parent_id
-------------------------------------------
1 0 (first parent)
5 1 (all children of first parent)
2 0 second parent
3 2 (all children of second parent)
4 0 third parent
6 0 fourth parent
I can use union of parents followed by all childs But that gives me parents first then the children. I need parent and immediately its children.
Anyone can help?