程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL 完成樹的遍歷詳解及復雜完成示例

MySQL 完成樹的遍歷詳解及復雜完成示例

編輯:MySQL綜合教程

MySQL 完成樹的遍歷詳解及復雜完成示例。本站提示廣大學習愛好者:(MySQL 完成樹的遍歷詳解及復雜完成示例)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL 完成樹的遍歷詳解及復雜完成示例正文


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 ; 

三、測試


CALL showChildLst(-1); 
CALL showChildLst(13); 
CALL showChildLst(14); 
CALL showChildLst(17); 
CALL showChildLst(18); 
 
CALL showParentLst(-1); 
CALL showParentLst(13); 
CALL showParentLst(14); 
CALL showParentLst(17); 
CALL showParentLst(18); 

四、遺留問題

1. 由於mysql對靜態游標的支持不夠,所以要想做成通用的進程或函數比擬困難,可以應用兩個暫時表來轉換(同時去掉了遞歸調用)是個絕對通用的完成。

2. 目前來看無論哪種完成,效率都不太好,希望mysql自己能完成Oracle 的connect by 功用,應該會比擬優化。

感激閱讀,希望能協助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved