`

Mysql 寻找父节点的存储过程

阅读更多

近日,考虑到在程序中递归查询速度较慢,公司写了一个虚拟目录寻找父节点的存储过程,以ID列出来,用,分割

 

-- Function "getparentlst" DDL
CREATE DEFINER=`root`@`localhost` FUNCTION `getparentlst`(myid INT) RETURNS varchar(1000) CHARSET utf8

 

begin
DECLARE sTemp INTEGER;
DECLARE sTempChd VARCHAR(1000);
set sTemp=myid;
set sTempChd='';
WHILE sTemp<>0 do
 select parentdir into sTemp from virsualdir where id =sTemp;
 if(sTemp<>0) then
  SET sTempChd = concat(sTemp,',',sTempChd);
 end if;
end WHILE;
set sTempChd=left(sTempChd,CHARACTER_LENGTH(sTempChd)-1);
RETURN sTempChd;
end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics