There is a table, for example, I need to add the Lubelskie Voivodeship to Poland, the very branch of Poland is empty. To create a country, I use this code, but it requires the ID of the left element, and we have a branch empty

BEGIN; SELECT @treeRight := rgt FROM geo WHERE id = 16; /* справа от ветки Польща */ UPDATE geo SET rgt = rgt + 2 WHERE rgt > @treeRight; UPDATE geo SET lft = lft + 2 WHERE lft > @treeRight; INSERT INTO geo VALUES(0, 'Росія', @treeRight + 1, @treeRight + 2, 2); COMMIT; 

enter image description here

    1 answer 1

     UPDATE geo SET geo.rgt = geo.rgt + 2, geo.lft = IF(geo.lft > $right_key, geo.lft + 2, geo.lft) WHERE geo.rgt >= $right_key INSERT INTO geo SET geo.lft = $right_key, geo.rgt = $right_key + 1, level = $level + 1, name = '$name' 

    More here