《MysqL必读MysqL递归查询(未分页版本)》要点:
本文介绍了MysqL必读MysqL递归查询(未分页版本),希望对您有用。如果有疑问,可以联系我们。
例子,MysqL递归查询代码.
MysqL数据库
CREATE table `treenodes` (
`ID` int(11) NOT NulL,
`nodename` varchar(20) DEFAulT NulL,
`pID` int(11) DEFAulT NulL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAulT CHARSET=utf8;
-- ----------------------------
-- Records of treenodes
-- ----------------------------
INSERT INTO `treenodes` VALUES ('1','A','0');
INSERT INTO `treenodes` VALUES ('2','B','1');
INSERT INTO `treenodes` VALUES ('3','C','1');
INSERT INTO `treenodes` VALUES ('4','D','2');
INSERT INTO `treenodes` VALUES ('5','E','2');
INSERT INTO `treenodes` VALUES ('6','F','3');
INSERT INTO `treenodes` VALUES ('7','G','6');
CREATE PROCEDURE showChildList (IN rootID INT,IN)
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;
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;
MysqL> call showChildList(1);
+-----+----+-------+----+----------+-----+
| sno | ID | depth | ID | nodename | pID |
+-----+----+-------+----+----------+-----+
| 2 | 1 | 0 | 1 | A | 0 |
| 3 | 2 | 1 | 2 | B | 1 |
| 4 | 4 | 2 | 4 | D | 2 |
| 5 | 5 | 2 | 5 | E | 2 |
| 6 | 3 | 1 | 3 | C | 1 |
| 7 | 6 | 2 | 6 | F | 3 |
| 8 | 7 | 3 | 7 | G | 6 |
+-----+----+-------+----+----------+-----+
7 rows in set
query OK,0 rows affected
MysqL> call showChildList(3);
+-----+----+-------+----+----------+-----+
| sno | ID | depth | ID | nodename | pID |
+-----+----+-------+----+----------+-----+
| 9 | 3 | 0 | 3 | C | 1 |
| 10 | 6 | 1 | 6 | F | 3 |
| 11 | 7 | 2 | 7 | G | 6 |
+-----+----+-------+----+----------+-----+
3 rows in set
query OK,0 rows affected
MysqL> call showChildList(5);
+-----+----+-------+----+----------+-----+
| sno | ID | depth | ID | nodename | pID |
+-----+----+-------+----+----------+-----+
| 12 | 5 | 0 | 5 | E | 2 |
+-----+----+-------+----+----------+-----+
1 row in set
query OK,0 rows affected
MysqL递归查询替代函数实例
MysqL递归查询树形叶子
MysqL 递归查询当前节点子节点
MysqL递归查询实现办法
sql递归查询代码(cte应用)
sql2005递归查询的例子
sql递归查询(with cte实现)
sql 递归查询的代码(图文)
sql server 递归查询数据MysqL数据库
内存溢出PHP培训学院每天发布《MysqL必读MysqL递归查询(未分页版本)》等实战技能,PHP、MysqL、liNUX、APP、Js,CSS全面培养人才。
总结以上是内存溢出为你收集整理的Mysql必读mysql递归查询(未分页版本)全部内容,希望文章能够帮你解决Mysql必读mysql递归查询(未分页版本)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)