I don't know if what I'm asking is even possible, but here's my situation. I have a table structured somewhat like this:
+--------------------------------------------------+
| id | parent_id | name | category | ....
+--------------------------------------------------+
| 0 | -1 | item0 | 1 |
| 1 | 0 | item1 | 1 |
| 2 | 0 | item2 | 1 |
| 3 | 2 | item3 | 1 |
| 4 | 2 | item4 | 1 |
| 5 | -1 | item5 | 1 |
+--------------------------------------------------+
A parent_id of -1 will mean it is a "base" item with no parent. Each item will have more columns of information. I need to somehow output all items in a category nested like the following:
item0 => item1
=> item2
=> item3
=> item4
item5
I don't know if that makes sense or not, but hopefully it does!
The only way I can think of doing this is making a query to get all of the "base" items (querying for rows with parent_id = -1) then iterate through every resulting row, querying for rows that have their parent_id equal to the current row's id, then repeating the process going deeper and deeper until there aren't any more children for a base item.
Is there a better way?
Thanks!!