views:

27

answers:

1

I am using nested sets to represent a tree in mysql, like so:

Tree
ID
title
lft
rgt

Given the ID of a node in the tree, what is the easiest / best way to UPDATE that node as well as all of it's ancestors?

For example, let's say that the node ID (36) is 4 levels deep in the tree. I would like to update its title, as well as every parent node's title, all the way to the root, to the word "fish". (Should be four updates in all.)

Thanks for your help!

A: 

I found an answer to this, so am posting it here in case anyone else encounters a similar problem.

Given the ID of an arbitrarily deep node, this will update the node and all ancestors of the node.

        UPDATE 

            Tree AS node,
            Tree AS parent

        SET parent.title = 'fish'

        WHERE 

            node.lft BETWEEN parent.lft AND parent.rgt
            AND node.ID = '$ID';
Travis