MySQL中实现类似Oracle的SYS_CONNECT_BY_PATH功能

Oracle中的SYS_CONNECT_BY_PATH函数为开发带来了便利,MySQL中如何实现类似的功能呢?

DELIMITER $$
 CREATE FUNCTION `getParentList` (rootId VARCHAR (50)) RETURNS VARCHAR (1000)
 BEGIN
  DECLARE sParentList VARCHAR (1000) ;
  DECLARE sParentTemp VARCHAR(1000);
  SET sParentTemp =CAST(rootId AS CHAR);
  WHILE sParentTemp IS NOT NULL DO
  IF (sParentList IS NOT NULL) THEN
  SET sParentList = CONCAT(sParentTemp,'/',sParentList);
  ELSE
  SET sParentList = CONCAT(sParentTemp);
  END IF;
 SELECT GROUP_CONCAT(parent_module_id) INTO sParentTemp FROM TOP_SYS_MODULE WHERE FIND_IN_SET(module_id,sParentTemp)>0;
 END WHILE;
 RETURN sParentList;
 END$$
 DELIMITER ; 


SELECT module_id,parent_module_id,getParentList(module_id) FROM TOP_SYS_MODULE;

相关推荐