数据库递归算法

Oracle

select XXX from table
start with XXX
connect by prior XXX

Mysql(需要自己写)

   1.先写一个 函数:


set global log_bin_trust_function_creators=TRUE;
DROP FUNCTION IF EXISTS `getChild`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getChild`(pid VARCHAR(8000)) RETURNS varchar(8000) CHARSET utf8
    COMMENT '获取所有孩子'
BEGIN
DECLARE sTemp VARCHAR(8000);
DECLARE sTempChd VARCHAR(8000);

SET sTemp='$';
SET sTempChd = pid;

WHILE sTempChd IS NOT NULL DO
SET sTemp= CONCAT(sTemp,',',sTempChd);
SELECT GROUP_CONCAT(id) INTO sTempChd FROM sys_organ WHERE del_flag='0' and FIND_IN_SET(parent_id,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
;;
DELIMITER ;

  2.FIND_IN_SET(id,getChild(“想要查找的父id”))

 

 

    原文作者:递归算法
    原文地址: https://blog.csdn.net/qq_16409287/article/details/81101727
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞