MySQL 获取无限级某级的全路径

传递参数:文件夹ID

 1 DROP FUNCTION IF EXISTS RecursionFolderFullPath;

 2 

 3 CREATE FUNCTION RecursionFolderFullPath(folderId INT(11)) 

 4 RETURNS VARCHAR(1000)

 5 BEGIN

 6 declare fullPath VARCHAR(1000) default '';

 7 declare parentId INT(11) default '0';

 8 

 9 select concat(folder_name,'/',fullPath),folder_parent_id into fullPath,parentId

10 from folder 

11 where folder_id=folderId;

12 

13 WHILE parentId > 0 DO

14 select concat(folder_name,'/',fullPath),folder_parent_id into fullPath,parentId

15 from folder 

16 where folder_id=parentId;

17 END WHILE;

18 

19 return fullPath; 

20 END;

调用:

 1 select RecursionFolderFullPath(67); 

结果:

 

你可能感兴趣的:(mysql)