I’m facing a problem with the nested set model, with MySQL.
I can insert, delete, move subtrees to another parent, everything works fine.
But I can’t figure out how to order siblings. For example, I have these siblings :
A, B, C, D, E
And I want to move B after D, obtaining this :
A, C, D, B, E
I found tons of stored procedures for inserting, deleting, etc. but not a single one to order siblings. The only one I found is a procedure for swapping siblings, but that’s not what I want to achieve.
I tried to write my own one, but it seems complicated and doesn’t work in all cases.
If you know how to move a node before or after one of his siblings, this would be greatly appreciated.
So… I re-wrote everything and here is a stored procedure that works well to move one node after one of his siblings. If we want to move the node to the first position, just pass the parent id in place of the sibling id.
Maybe that’s not the most beautiful piece of code we’ve ever seen, but it works fine and is probably much more efficient than any kind of sorting algorithm, for example.