首页 > 数据库 > MySQL > 正文

MySQL 实现树的遍历详解及简单实现示例

2024-07-24 12:52:55
字体:
来源:转载
供稿:网友

MySQL 实现树的遍历

经常在一个表中有父子关系的两个字段,比如empno与manager,这种结构中需要用到树的遍历。在Oracle 中可以使用connect by简单解决问题,但MySQL 5.1中还不支持(据说已纳入to do中),要自己写过程或函数来实现。

一、建立测试表和数据:

DROP TABLE IF EXISTS `channel`; CREATE TABLE `channel` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(200) DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8; /*Data for the table `channel` */ insert into `channel`(`id`,`cname`,`parent_id`) values (13,'首页',-1), (14,'TV580',-1), (15,'生活580',-1), (16,'左上幻灯片',13), (17,'帮忙',14), (18,'栏目简介',17);

 二、利用临时表和递归过程实现树的遍历(MySQL的UDF不能递归调用):

DELIMITER $$ USE `db1`$$ -- 从某节点向下遍历子节点 -- 递归生成临时表数据 DROP PROCEDURE IF EXISTS `createChildLst`$$ CREATE PROCEDURE `createChildLst`(IN rootId INT,IN nDepth INT) BEGIN DECLARE done INT DEFAULT 0; DECLARE b INT; DECLARE cur1 CURSOR FOR SELECT id FROM channel WHERE parent_id=rootId; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET max_sp_recursion_depth=12; INSERT INTO tmpLst VALUES (NULL,rootId,nDepth); OPEN cur1; FETCH cur1 INTO b; WHILE done=0 DO CALL createChildLst(b,nDepth+1); FETCH cur1 INTO b; END WHILE; CLOSE cur1; END$$ -- 从某节点向上追溯根节点 -- 递归生成临时表数据 DROP PROCEDURE IF EXISTS `createParentLst`$$ CREATE PROCEDURE `createParentLst`(IN rootId INT,IN nDepth INT) BEGIN DECLARE done INT DEFAULT 0; DECLARE b INT; DECLARE cur1 CURSOR FOR SELECT parent_id FROM channel WHERE id=rootId; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET max_sp_recursion_depth=12; INSERT INTO tmpLst VALUES (NULL,rootId,nDepth); OPEN cur1; FETCH cur1 INTO b; WHILE done=0 DO CALL createParentLst(b,nDepth+1); FETCH cur1 INTO b; END WHILE; CLOSE cur1; END$$ -- 实现类似Oracle SYS_CONNECT_BY_PATH的功能 -- 递归过程输出某节点id路径 DROP PROCEDURE IF EXISTS `createPathLst`$$ CREATE PROCEDURE `createPathLst`(IN nid INT,IN delimit VARCHAR(10),INOUT pathstr VARCHAR(1000)) BEGIN DECLARE done INT DEFAULT 0; DECLARE parentid INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT t.parent_id,CONCAT(CAST(t.parent_id AS CHAR),delimit,pathstr) FROM channel AS t WHERE t.id = nid; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET max_sp_recursion_depth=12; OPEN cur1; FETCH cur1 INTO parentid,pathstr; WHILE done=0 DO CALL createPathLst(parentid,delimit,pathstr); FETCH cur1 INTO parentid,pathstr; END WHILE; CLOSE cur1; END$$ -- 递归过程输出某节点name路径 DROP PROCEDURE IF EXISTS `createPathnameLst`$$ CREATE PROCEDURE `createPathnameLst`(IN nid INT,IN delimit VARCHAR(10),INOUT pathstr VARCHAR(1000)) BEGIN DECLARE done INT DEFAULT 0; DECLARE parentid INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT t.parent_id,CONCAT(t.cname,delimit,pathstr) FROM channel AS t WHERE t.id = nid; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET max_sp_recursion_depth=12; OPEN cur1; FETCH cur1 INTO parentid,pathstr; WHILE done=0 DO CALL createPathnameLst(parentid,delimit,pathstr); FETCH cur1 INTO parentid,pathstr; END WHILE; CLOSE cur1; END$$ -- 调用函数输出id路径 DROP FUNCTION IF EXISTS `fn_tree_path`$$ CREATE FUNCTION `fn_tree_path`(nid INT,delimit VARCHAR(10)) RETURNS VARCHAR(2000) CHARSET utf8 BEGIN DECLARE pathid VARCHAR(1000); SET @pathid=CAST(nid AS CHAR); CALL createPathLst(nid,delimit,@pathid); RETURN @pathid; END$$ -- 调用函数输出name路径 DROP FUNCTION IF EXISTS `fn_tree_pathname`$$ CREATE FUNCTION `fn_tree_pathname`(nid INT,delimit VARCHAR(10)) RETURNS VARCHAR(2000) CHARSET utf8 BEGIN DECLARE pathid VARCHAR(1000); SET @pathid=''; CALL createPathnameLst(nid,delimit,@pathid); RETURN @pathid; END$$ -- 调用过程输出子节点 DROP PROCEDURE IF EXISTS `showChildLst`$$ CREATE PROCEDURE `showChildLst`(IN rootId INT) BEGIN DROP TEMPORARY TABLE IF EXISTS tmpLst; CREATE TEMPORARY TABLE IF NOT EXISTS tmpLst (sno INT PRIMARY KEY AUTO_INCREMENT,id INT,depth INT); CALL createChildLst(rootId,0); SELECT channel.id,CONCAT(SPACE(tmpLst.depth*2),'--',channel.cname) NAME,channel.parent_id,tmpLst.depth,fn_tree_path(channel.id,'/') path,fn_tree_pathname(channel.id,'/') pathname FROM tmpLst,channel WHERE tmpLst.id=channel.id ORDER BY tmpLst.sno; END$$ -- 调用过程输出父节点 DROP PROCEDURE IF EXISTS `showParentLst`$$ CREATE PROCEDURE `showParentLst`(IN rootId INT) BEGIN DROP TEMPORARY TABLE IF EXISTS tmpLst; CREATE TEMPORARY TABLE IF NOT EXISTS tmpLst (sno INT PRIMARY KEY AUTO_INCREMENT,id INT,depth INT); CALL createParentLst(rootId,0); SELECT channel.id,CONCAT(SPACE(tmpLst.depth*2),'--',channel.cname) NAME,channel.parent_id,tmpLst.depth,fn_tree_path(channel.id,'/') path,fn_tree_pathname(channel.id,'/') pathname FROM tmpLst,channel WHERE tmpLst.id=channel.id ORDER BY tmpLst.sno; END$$ DELIMITER ;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表