MySQL Forums
Forum List  »  Knowledge Base

Re: Count Hierarchical Data(Binary Tree) in MySQL?
Posted by: Peter Brawley
Date: January 27, 2010 10:19AM

A leaf is a node that has no child nodes. Your 14 &c example counts are child node counts, not leaf node counts. To retrieve those counts, try something like ...

SELECT parent.id, Count(distinct child.id) - 1 AS 'Child Nodes'
FROM tbl parent
JOIN tbl child ON child.left_id BETWEEN parent.left_id AND parent right_id
GROUP BY parent.id;

PB
http://www.artfulsoftware.com

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Count Hierarchical Data(Binary Tree) in MySQL?
2722
January 27, 2010 10:19AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.