Nested Dial Model: Insert Node at the End of SubNodes

Existing data (name, lft, rgt):

Root, 1, 4
Item1, 2, 3

It looks like:

- Root
--- Item1

How do you insert a new node (Item2) BELOW Item1? My current system logic matches most of the examples I found on the Internet, but the result is Item2 ABOVE Item1.

- Root
--- Item1
--- Item2

Thanks for the help.

+3
source share
3 answers

Think of the model of nested sets as a file XMLwith lftand rgt, which are the strings in which the current and end tags are located:

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

To insert a new subtag in root, you will need to shift all subsequent entries:

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

, item2.lft item2.rgt ( item2.rgt + 1 item1.rgt + 2), lft rgt all, , item1.rgt:

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

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

, " " , : set item2.lft = root.rgt(4) set item2.rgt = root.rgt + 1 (5)

mytable SET rgt = rgt + 2 WHERE rgt >= root.rgt root.rgt 6;

mytable SET lft = lft + 2 WHERE lft > root.rgt root.lft (1), , .

lft rgt.

+2
Root, 1, 6
Item1, 2, 3
Item2, 4, 5

ORDER BY ItemName, , , .

, . 5- : http://intelligent-enterprise.informationweek.com/001020/celko.jhtml;jsessionid=OOU0L1TIM1IB1QE1GHPSKH4ATMY32JVN

, UPDATE, . , . , .

+1

Source: https://habr.com/ru/post/1730868/


All Articles