For example I have the following MySQL table:
parent_id | child_id
---------------------
1 | 4
1 | 3
1 | 5
2 | 8
3 | 7
I want to print out the parent and all its children in a format like below:
parent | child
---------------------
| 4
1 | 3
| 5
---------------------
2 | 8
---------------------
3 | 7
Basically I just want to display the parent ONCE(Distinct) and list out all its children with PHP. Is it possible to retrieve the above result with just ONE SQL query? I can get the above result if I first query the parents and then recursively query the children using the parent ids but that would be alot more SQL queries hitting the DB.
Or, do I retrieve the result containing every parent_id & children_id and achieve the above result in PHP by using arrays. If so, please tell me how.
Yes. Select normally and use the parents as keys in an array.
Or something similar.
EDIT
The display part would look something like this: