Home > Database > Mysql Tutorial > mysql中实现相仿oracle的SYS_CONNECT_BY_PATH功能

mysql中实现相仿oracle的SYS_CONNECT_BY_PATH功能

WBOY
Release: 2016-06-07 16:25:11
Original
2624 people have browsed it

mysql中实现类似oracle的SYS_CONNECT_BY_PATH功能 oracle中的SYS_CONNECT_BY_PATH函数为开发带来了便利,mysql中如何实现类的功能呢? DELIMITER $$ CREATE FUNCTION `getParentList` (rootId VARCHAR (50)) RETURNS VARCHAR (1000) BEGIN DECLARE sParentLis

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; 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template