通过左、右值的SQL查询即可实现, 以B为例 : SELECT COUNT(*) FROM Tree WHERE Lft <= 2 AND Rgt >=13
六、定义计算指定节点所在层的函数CountLayer
CREATE DEFINER=`root`@`localhost` FUNCTION `CountLayer`(p_node_id int) RETURNS int(11)
BEGIN
declare p_result,p_lft,p_rgt int default 0;
if exists (select 1 from tree where node_id=p_node_id) then
begin
select lft, rgt into p_lft, p_rgt from tree where node_id=p_node_id;
select count(*) into p_result from tree where lft <= p_lft and rgt >= p_rgt;
end;
return p_result;
end if;
RETURN 0;
END
七、定义层次试图Tree_View
基于刚刚定义的层计算函数来创建一个包含层次字段的试图。
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `tree_view` AS
SELECT
`tree`.`node_id` AS `node_id`,
`tree`.`name` AS `name`,
`tree`.`lft` AS `lft`,
`tree`.`rgt` AS `rgt`,
COUNTLAYER(`tree`.`node_id`) AS `layer`
FROM
`tree`
ORDER BY `tree`.`lft`
八、 获取所有子孙节点的存储过程GetChildrenNodeList
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetChildrenNodeList`(in p_node_id int)
BEGIN
declare p_lft,p_rgt int default 0;
if exists (select node_id from tree where node_id=p_node_id) then
begin
select lft,rgt into p_lft,p_rgt from tree where node_id=p_node_id;
select * from Tree_View where lft between p_lft and p_rgt order by layer, lft;
end;
end if;
END
九、 查询B的所有子孙节点
call GetChildrenNodeList(2);
十、 获取所有父节点的存储过程GetParentNodePath
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetParentNodePath`(in p_node_id int)
BEGIN
declare p_lft,p_rgt int default 0;
if exists (select node_id from tree where node_id=p_node_id) then
begin
select lft,rgt into p_lft,p_rgt from tree where node_id=p_node_id;
select * from Tree_View where lft<p_lft and rgt>p_rgt order by layer,lft asc;
end;
end if;
END
以E节点为例:
call GetParentNodePath(5);
添加节点的存储过程AddSubNode
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddSubNode`(in p_node_id int,in p_node_name varchar(50))
BEGIN
declare p_rgt int default 0;
if exists(select node_id from tree where node_id=p_node_id) then
begin
SET AUTOCOMMIT=0;
START TRANSACTION;
select rgt into p_rgt from tree where node_id=p_node_id;
update tree set rgt=rgt+2 where rgt>=p_rgt;
update tree set lft=lft+2 where lft>=p_rgt;
insert into tree(name,lft,rgt) values(p_node_name,p_rgt,p_rgt+1);
COMMIT;
end;
end if;
END
调用AddSubNode存储过程
call AddSubNode(8,'K');
十二、 删除节点
删除节点是新增节点的逆向过程。
创建删除节点存储过程DelNode。
CREATE DEFINER=`root`@`localhost` PROCEDURE `DelNode`(in p_node_id int)
BEGIN
declare p_lft,p_rgt int default 0;
if exists(select p_node_id from tree where node_id =p_node_id) then
START TRANSACTION;
select lft,rgt into p_lft,p_rgt from tree where node_id=p_node_id;
delete from tree where lft>=p_lft and rgt<=p_rgt;
update tree set lft=lft-(p_rgt - p_lft + 1) where lft > p_lft;
update tree set rgt=rgt-(p_rgt - p_lft + 1) where rgt > p_rgt;
COMMIT;
end if;
END
以删除C节点为例
call DelNode(3);