I have this table:
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`category_id` int(11) default NULL,
`root_id` int(11) default NULL
I got it.
All you need to do is set root_id to the top parents too, so that you can ORDER BY correctly.
With the query bellow I can have separeted trees, and uptade only the tree that I'm working on:
SELECT c . * , count( p.id ) AS depth
FROM `categories` c
CROSS JOIN categories p
WHERE (
c.lft
BETWEEN p.lft
AND p.rht
)
AND c.root_id = p.root_id
GROUP BY c.id
ORDER BY c.root_id, c.lft