set max_sp_recursion_depth=255;
call showChildLst(56);
DROP PROCEDURE IF EXISTS `createChildLst`;
delimiter ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `createChildLst`(IN pid INT,IN nDepth INT)
COMMENT '入口过程'
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE b INT;
DECLARE cur1 CURSOR FOR SELECT id FROM sheet1 where parent_id=pid order by sort;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; ### 这里相当于定义了一种异步事件,当事件发生会设置变量值
OPEN cur1;
FETCH cur1 INTO b;
INSERT INTO tmpLst VALUES (NULL,pid,nDepth,done);
WHILE done=0 DO
CALL createChildLst(b,nDepth+1);
FETCH cur1 INTO b;
END WHILE;
CLOSE cur1;
END
;;
delimiter ;
-- ----------------------------
-- Procedure structure for `showChildLst`
-- ----------------------------
DROP PROCEDURE IF EXISTS `showChildLst`;
delimiter ;;
CREATE DEFINER=`root`@`lo
MySQL 存储过程实现 id-pid结构的树形数据展示
最新推荐文章于 2024-04-30 14:53:22 发布
本文详细介绍了如何使用MySQL存储过程来处理id-pid结构的数据,将其转化为树形展示。通过递归和自连接,实现了从数据库中获取并组织层级关系数据的方法。
摘要由CSDN通过智能技术生成