在Oracle 中咱們知道有一個 Hierarchical Queries 經過CONNECT BY 咱們能夠方便的查了全部當前節點下的全部子節點。但很遺憾,在MySQL的目前版本中尚未對應的功能。node
在MySQL中若是是有限的層次,好比咱們事先若是能夠肯定這個樹的最大深度是4, 那麼全部節點爲根的樹的深度均不會超過4,則咱們能夠直接經過left join 來實現。mysql
但不少時候咱們沒法控制樹的深度。這時就須要在MySQL中用存儲過程來實現或在你的程序中來實現這個遞歸。本文討論一下幾種實現的方法。sql
樣例數據:數據庫
mysql> create table treeNodes
-> (
-> id int primary key,
-> nodename varchar(20),
-> pid int
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> select * from treenodes;
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
| 8 | H | 0 |
| 9 | I | 8 |
| 10 | J | 8 |
| 11 | K | 8 |
| 12 | L | 9 |
| 13 | M | 9 |
| 14 | N | 12 |
| 15 | O | 12 |
| 16 | P | 15 |
| 17 | Q | 15 |
+----+----------+------+
17 rows in set (0.00 sec)oracle
樹形圖以下函數
1:A
+-- 2:B
| +-- 4:D
| +-- 5:E
+-- 3:C
+-- 6:F
+-- 7:G
8:H
+-- 9:I
| +-- 12:L
| | +--14:N
| | +--15:O
| | +--16:P
| | +--17:Q
| +-- 13:M
+-- 10:J
+-- 11:K post
方法一:利用函數來獲得全部子節點號。spa
建立一個function getChildLst, 獲得一個由全部子節點號組成的字符串. 排序
mysql> delimiter //
mysql>
mysql> CREATE FUNCTION `getChildLst`(rootId INT)
-> RETURNS varchar(1000)
-> BEGIN
-> DECLARE sTemp VARCHAR(1000);
-> DECLARE sTempChd VARCHAR(1000);
->
-> SET sTemp = '$';
-> SET sTempChd =cast(rootId as CHAR);
->
-> WHILE sTempChd is not null DO
-> SET sTemp = concat(sTemp,',',sTempChd);
-> SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)>0;
-> END WHILE;
-> RETURN sTemp;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;遞歸
使用咱們直接利用find_in_set函數配合這個getChildlst來查找
mysql> select getChildLst(1);
+-----------------+
| getChildLst(1) |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from treeNodes
-> where FIND_IN_SET(id, getChildLst(1));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
7 rows in set (0.01 sec)
mysql> select * from treeNodes
-> where FIND_IN_SET(id, getChildLst(3));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 3 | C | 1 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
3 rows in set (0.01 sec)
優勢: 簡單,方便,沒有遞歸調用層次深度的限制 (max_sp_recursion_depth,最大255) ;
缺點:長度受限,雖然能夠擴大 RETURNS varchar(1000),但老是有最大限制的。
MySQL目前版本( 5.1.33-community)中還不支持function 的遞歸調用。
方法二:利用臨時表和過程遞歸
建立存儲過程以下。createChildLst 爲遞歸過程,showChildLst爲調用入口過程,準備臨時表及初始化。
mysql> delimiter //
mysql>
mysql> # 入口過程
mysql> CREATE PROCEDURE showChildLst (IN rootId INT)
-> BEGIN
-> CREATE TEMPORARY TABLE IF NOT EXISTS tmpLst
-> (sno int primary key auto_increment,id int,depth int);
-> DELETE FROM tmpLst;
->
-> CALL createChildLst(rootId,0);
->
-> select tmpLst.*,treeNodes.* from tmpLst,treeNodes where tmpLst.id=treeNodes.id order by tmpLst.sno;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> # 遞歸過程
mysql> 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 treeNodes where pid=rootId;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
->
-> 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;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
調用時傳入結點
mysql> call showChildLst(1);
+-----+------+-------+----+----------+------+
| sno | id | depth | id | nodename | pid |
+-----+------+-------+----+----------+------+
| 4 | 1 | 0 | 1 | A | 0 |
| 5 | 2 | 1 | 2 | B | 1 |
| 6 | 4 | 2 | 4 | D | 2 |
| 7 | 5 | 2 | 5 | E | 2 |
| 8 | 3 | 1 | 3 | C | 1 |
| 9 | 6 | 2 | 6 | F | 3 |
| 10 | 7 | 3 | 7 | G | 6 |
+-----+------+-------+----+----------+------+
7 rows in set (0.13 sec)
Query OK, 0 rows affected, 1 warning (0.14 sec)
mysql>
mysql> call showChildLst(3);
+-----+------+-------+----+----------+------+
| sno | id | depth | id | nodename | pid |
+-----+------+-------+----+----------+------+
| 1 | 3 | 0 | 3 | C | 1 |
| 2 | 6 | 1 | 6 | F | 3 |
| 3 | 7 | 2 | 7 | G | 6 |
+-----+------+-------+----+----------+------+
3 rows in set (0.11 sec)
Query OK, 0 rows affected, 1 warning (0.11 sec)
depth 爲深度,這樣能夠在程序進行一些顯示上的格式化處理。相似於oracle中的 level 僞列。sno 僅供排序控制。這樣你還能夠經過臨時表tmpLst與數據庫中其它表進行聯接查詢。
MySQL中你能夠利用系統參數 max_sp_recursion_depth 來控制遞歸調用的層數上限。以下例設爲12.
mysql> set max_sp_recursion_depth=12;
Query OK, 0 rows affected (0.00 sec)
優勢 : 能夠更靈活處理,及層數的顯示。而且能夠按照樹的遍歷順序獲得結果。
缺點 : 遞歸有255的限制。
方法三:利用中間表和過程
(本方法由yongyupost2000提供樣子改編)
建立存儲過程以下。因爲MySQL中不容許在同一語句中對臨時表屢次引用,只以使用普通表tmpLst來實現了。固然你的程序中負責在用完後清除這個表。
delimiter //
drop PROCEDURE IF EXISTS showTreeNodes_yongyupost2000//
CREATE PROCEDURE showTreeNodes_yongyupost2000 (IN rootid INT)
BEGIN
DECLARE Level int ;
drop TABLE IF EXISTS tmpLst;
CREATE TABLE tmpLst (
id int,
nLevel int,
sCort varchar(8000)
);
Set Level=0 ;
INSERT into tmpLst SELECT id,Level,ID FROM treeNodes WHERE PID=rootid;
WHILE ROW_COUNT()>0 DO
SET Level=Level+1 ;
INSERT into tmpLst
SELECT A.ID,Level,concat(B.sCort,A.ID) FROM treeNodes A,tmpLst B
WHERE A.PID=B.ID AND B.nLevel=Level-1 ;
END WHILE;
END;
//
delimiter ;
CALL showTreeNodes_yongyupost2000(0);
執行完後會產生一個tmpLst表,nLevel 爲節點深度,sCort 爲排序字段。
使用方法
SELECT concat(SPACE(B.nLevel*2),'+--',A.nodename)
FROM treeNodes A,tmpLst B
WHERE A.ID=B.ID
ORDER BY B.sCort;
+--------------------------------------------+ | concat(SPACE(B.nLevel*2),'+--',A.nodename) | +--------------------------------------------+ | +--A | | +--B | | +--D | | +--E | | +--C | | +--F | | +--G | | +--H | | +--J | | +--K | | +--I | | +--L | | +--N | | +--O | | +--P | | +--Q | | +--M | +--------------------------------------------+ 17 rows in set (0.00 sec)