Move node in nested set

后端 未结 13 902
孤街浪徒
孤街浪徒 2020-12-07 15:06

I\'d need a MySQL query that moves a node and all its children within a nested set. I found this site, but that function just seems so illogical - there\'s no universe

13条回答
  •  时光说笑
    2020-12-07 15:28

    Moving subtrees around is very expensive and complex in the Nested Sets design.

    You should consider a different design for representing trees.

    For example, if you use the Path Enumeration design, you store the list of direct ancestors of each node as a concatenated string.

    id path
     1  1/
     2  1/2/
     3  1/3/
     4  1/3/4/
     5  1/3/5/
    

    Then moving a subtree (say node 3 moves to be a child of node 2):

    UPDATE Tree t
     JOIN Tree node2 ON (node2.id = 2)
     JOIN Tree node3 ON (node3.id = 3)
    SET t.path = CONCAT(node2.path, REPLACE(t.path, node3.path, node2.path))
    WHERE t.path LIKE CONCAT(node3.path, '%');
    

提交回复
热议问题