這裏......
要插入:
begin;
SELECT rgt INTO new_lft FROM tree_map WHERE node_id = parent_id;
UPDATE tree_map SET rgt = rgt + 2 WHERE rgt >= new_lft;
UPDATE tree_map SET lft = lft + 2 WHERE lft > new_lft;
INSERT INTO tree_map (lft, rgt, parent_id) VALUES (new_lft, (new_lft + 1), parent_id);
commit;
SELECT LAST_INSERT_ID();
刪除:
SELECT lft, rgt, (rgt - lft), (rgt - lft + 1), parent_id
INTO new_lft, new_rgt, has_leafs, width, superior_parent
FROM tree_map WHERE node_id = pnode_id;
begin;
DELETE FROM tree_content WHERE node_id = pnode_id;
IF (has_leafs = 1) THEN
DELETE FROM tree_map WHERE lft BETWEEN new_lft AND new_rgt;
UPDATE tree_map SET rgt = rgt - width WHERE rgt > new_rgt;
UPDATE tree_map SET lft = lft - width WHERE lft > new_rgt;
ELSE
DELETE FROM tree_map WHERE lft = new_lft;
UPDATE tree_map SET rgt = rgt - 1, lft = lft - 1, parent_id = superior_parent
WHERE lft BETWEEN new_lft AND new_rgt;
UPDATE tree_map SET rgt = rgt - 2 WHERE rgt > new_rgt;
UPDATE tree_map SET lft = lft - 2 WHERE lft > new_rgt;
END IF;
commit;
你也可能需要使用驚人的存儲過程的MySQL,而不:
https://github.com/werc/TreeTraversal/blob/master/sql/r_tree_traversal.sql#L4