嵌套集模型:在子节点末尾插入节点(Nested Set Model: Inserting Node at the end of SubNodes)

编程入门 行业动态 更新时间:2024-10-22 19:37:54
嵌套集模型:在子节点末尾插入节点(Nested Set Model: Inserting Node at the end of SubNodes)

现有数据(name,lft,rgt):

Root, 1, 4 Item1, 2, 3

好像:

- Root --- Item1

如何在项目1下面插入一个新节点(Item2)? 我的系统当前逻辑遵循我在网上找到的大多数示例,但结果是Item2 ABOVE Item1。

- Root --- Item1 --- Item2

感谢您的帮助。

Existing Data (name, lft, rgt):

Root, 1, 4 Item1, 2, 3

Looks like:

- Root --- Item1

How do you insert a new node (Item2) BELOW Item1? My system's current logic follows most examples I've found online but the result is Item2 ABOVE Item1.

- Root --- Item1 --- Item2

Thank you for the help.

最满意答案

将嵌套集模型视为XML文件,其中lft和rgt是起始和结束标记所在的行:

1 <root> 2 <item1> 3 </item1> 4 </root>

要在root插入新的子标签,您需要将所有后续记录向下移动:

1 <root> 2 <item1> 3 </item1> 4 <item2> 5 </item2> 6 </root>

所以你需要计算item2.lft和item2.rgt (相应的是item2.rgt + 1和item1.rgt + 2 ),然后增加所有大于item1.rgt项目的lft和rgt item1.rgt :

UPDATE mytable SET rgt = rgt + 2 WHERE rgt > item1.rgt UPDATE mytable SET lft = lft + 2 WHERE lft > item1.rgt

Think of the nested sets model as of an XML file with lft and rgt being the lines where the staring and ending tags reside:

1 <root> 2 <item1> 3 </item1> 4 </root>

To insert a new subtag into the root, you'll need to shift down all subsequent records:

1 <root> 2 <item1> 3 </item1> 4 <item2> 5 </item2> 6 </root>

So you'll need to calculate the item2.lft and item2.rgt (which are the item2.rgt + 1 and item1.rgt + 2, accordingly), and then increment all lft and rgt of all items which are greater than the item1.rgt:

UPDATE mytable SET rgt = rgt + 2 WHERE rgt > item1.rgt UPDATE mytable SET lft = lft + 2 WHERE lft > item1.rgt

更多推荐

本文发布于:2023-08-07 04:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   嵌套   末尾   模型   Nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!