I have a table setup categories, for example:
ID CatName CatParent
1 Websites NULL
2 Recipes NULL
3 Programming 1
4 Helpful 3
5 Useless 3
6 Desserts 2
If I have a category identifier, I would like to query the database to select the category and all parents in ancestral order. Each category has CatParent, which is the parent, or NULLif the parent does not exist.
So, for example, if I have a category 4 identifier, I would like the request to return:
array('4','3','1');
Or if I have a category 6 identifier:
array('6','2');
Or category 1 identifier:
array('1');
How do I build this query?
source
share