I've a tree structure of categories stored in the mySQL table with category_id
and parent_id
relation. Parent_id = Null
corresponds to the root node.
Category (category_id, category_name, parent_id)
What I'm trying to do is to fetch all the leaf nodes give the category_id of a node. I've followed this article. It discusses about getting all the leaf nodes with the below query:
SELECT t1.category_name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent_id
WHERE t2.category_id IS NULL;
But I'm trying to fetch the leaf nodes of a subtree. For example:
In the above structure given node 3 the results will be: 9, 10, 7, 11, 12, 13.
I've also tried the solution given here: adjacency model , given an id return the leaf nodes. But I'm not able to get the desired result.
Can you help me to find a solution?
Oh, well... I happen to have found a solution... It's a little awkward, however:
It works as long as
category_id
is < 100, that is, just two digits, but that can be easily adapted. Yes, a completely crazy/awkward/(you name it) solution, but it works for my purposes.