mysql7笔记----遍历节点所有子节点

时间:2021-05-14 10:24:02
mysql遍历节点的所有子节点
DELIMITER //
CREATE FUNCTION `getChildrenList`(rootId INT)
RETURNS VARCHAR(500)
BEGIN
DECLARE sTemp VARCHAR(500);
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 employees WHERE FIND_IN_SET(parent_id,sTempChd)>0;
END WHILE;
RETURN sTemp;
END SELECT getChildrenList(2);
FIND_IN_SET(str,strlist): 假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间,否则返回0
GROUP_CONCAT(id) INTO sTempChd:把无重复id放进sTempChd:

mysql7笔记----遍历节点所有子节点