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 universeid or treeid in a nested set model, and the code itself is just longer than what feels required. The only extra column I’ve got in the table is parent.
I couldn’t just remove and add the node again since it will loose its ID.
I see, that this topic is quite old, but anyway it’s still unanswered. I got here from Google, and found no direct answer to this question.
So, after a little research I found quite easy solution.
Everything, what we gonna need to move our node is: node left and right positions, new parent node right position. The node to the new position then can be moved in four easy steps:
which are equal to current ones by module.
That’s theory, now – this algorithm realization in MySQL (example using PHP):
Please beware – there still may be some syntax errors in SQL code, because I actually use this algorithm in PHP like this:
Please note also, that code may be optimized, but I going to leave it like that for better readability. Also consider table locking if you are using nested sets in multi-user systems.
Hope that my message will help to anyone, who will search for a solution after me. Any comments and corrections are also welcome.