Move node in nested set

后端 未结 13 975
孤街浪徒
孤街浪徒 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:35

    I know this post is old but im posting this solution for every one else that will get here to see a solution.I found this @ sedna-soft.de . I tested id and works perfectly

     -- moves a subtree before the specified position
     -- if the position is the rgt of a node, the subtree will be its last child
     -- if the position is the lft of a node, the subtree will be inserted before
     -- @param l the lft of the subtree to move
     -- @param r the rgt of the subtree to move
     -- @param p the position to move the subtree before
    
    
    
    
     SET @r: , @l: , @p: 
    
     update tree
     set
     lft = lft + if (@p > @r,
        if (@r < lft and lft < @p,
            @l - @r - 1,
            if (@l <= lft and lft < @r,
                @p - @r - 1,
                0
            )
        ),
        if (@p <= lft and lft < @l,
            @r - @l + 1,
            if (@l <= lft and lft < @r,
                @p - @l,
                0
            )
        )
    ),
    rgt = rgt + if (@p > @r,
        if (@r < rgt and rgt < @p,
            @l - @r - 1,
            if (@l < rgt and rgt <= @r,
                @p - @r - 1,
                0
            )
        ),
        if (@p <= rgt and rgt < @l,
            @r - @l + 1,
            if (@l < rgt and rgt <= @r,
                @p - @l,
                0
            )
        )
            )
      where @r < @p or @p < @l; 
    

提交回复
热议问题