更新嵌套集中的祖先?

发布于 2024-09-03 10:10:20 字数 224 浏览 4 评论 0原文

我使用嵌套集来表示 mysql 中的树,如下所示:

Tree
ID
title
lft
rgt

给定树中节点的 ID,更新该节点及其祖先的最简单/最佳方法是什么?

例如,假设节点 ID (36) 在树中的深度为 4 层。我想更新它的标题以及每个父节点的标题,一直到根,更新到单词“fish”。 (总共应该有四次更新。)

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-09-10 10:10:20

我找到了这个问题的答案,所以我将其发布在这里,以防其他人遇到类似的问题。

给定任意深度节点的 ID,这将更新该节点以及该节点的所有祖先。

        UPDATE 

            Tree AS node,
            Tree AS parent

        SET parent.title = 'fish'

        WHERE 

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

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';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文