嵌套集模型分层数据 [英] The Nested Set Model hierarchical data

查看:52
本文介绍了嵌套集模型分层数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题.

我的数据库结构是:

id | name | left | right // no need to tell me that left, right are reserved keywords

数据库中唯一的数据:

1 | family | 1 | 2

现在,我想为家庭添加新的 child:mother.所以理论上我应该为我想要添加子元素的元素取正确的值并在它之后释放一些空间.

Now, I want to add new child to the family: mother. So theoretically I should take the right value of the element to which I want to add the child and free-up some space after it.

UPDATE `hp_tree` SET `right`=`right`+2 WHERE `right` > 2;
UPDATE `hp_tree` SET `left`=`left`+2 WHERE `left` > 2;

然后简单地插入子mother:

INSERT INTO `hp_tree` SET `left`=2, `right`=3, `name`='Mother';

现在,问题是,这样family(根元素)right 值不会更新.我做错了吗?

Now, the problem is, that this way the family (root element) right value is not updated. Am I doing it wrong?

推荐答案

好的.我在在 MySQL 中管理分层数据 中对此进行了解释.参见以如果我们想要添加一个节点作为一个没有现有子节点的节点的子节点的部分.

Ok. I found this explained in Managing Hierarchical Data in MySQL. See the part starting with If we instead want to add a node as a child of a node that has no existing children.

这篇关于嵌套集模型分层数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆