If I have a category tree where there is an undetermined level of subcategories, is it possible to get the entire tree of categories in one query (without any loops)?
This is a possible scenario:
category_id parent_id Name
0 null Home
1 null Auto
2 0 Living Room
3 2 Couches
4 2 TV
5 4 Home Theatre
6 5 Cables
If you notice, Cables lineage is such that: Home > Living Room > TV > Home Theatre > Cables
Right now, I get the parent category of the current category and keep looping until I get to the parent category that has a null parent category. I know this part has been asked before. However, I'm wondering is there a better way to structure this table so that I don't have to run X amount of queries (or subqueries)? I considered for instance adding a 4th column called parents or something that would have the entire lineage in a comma separated list (such that in this case parents would be: 5,4,2,0
. However this seemed tacky and inefficient (and easy to break). Is there a better mechanism available?