mysql查询树状目录所有子节点

DELIMITER //
 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_DIRECTORY) INTO sTempChd FROM r_directory WHERE FIND_IN_SET(ID_DIRECTORY_PARENT,sTempChd)>0;
    END WHILE;
    RETURN sTemp;
   END
   //
   
SELECT * FROM r_directory WHERE FIND_IN_SET(ID_DIRECTORY, getChildLst(1));

  

posted @ 2018-04-26 15:30  迷途_小羔羊  阅读(769)  评论(0编辑  收藏  举报