Welcome Guest, Not a member yet? Register   Sign In
Nested Set - Depth of a Sub-Tree - Active Record
#1

[eluser]Otemu[/eluser]
Hi,

After reading the excellent managing hierarchical data in mysql I was wondering if anyone knows how to write the query below in active record or if it even possible with AR:

Code:
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
        nested_category AS parent,
        nested_category AS sub_parent,
        (
                SELECT node.name, (COUNT(parent.name) - 1) AS depth
                FROM nested_category AS node,
                nested_category AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.name = 'PORTABLE ELECTRONICS'
                GROUP BY node.name
                ORDER BY node.lft
        )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.name = sub_tree.name
GROUP BY node.name
ORDER BY node.lft;

Since all my queries currently in AR be nice to continue that trend, however if not then I guess I have to run this one as standard mysql.

Any help be appreciated.


Messages In This Thread
Nested Set - Depth of a Sub-Tree - Active Record - by El Forum - 10-10-2012, 05:44 AM
Nested Set - Depth of a Sub-Tree - Active Record - by El Forum - 10-10-2012, 05:51 AM
Nested Set - Depth of a Sub-Tree - Active Record - by El Forum - 10-10-2012, 07:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB