根据子节点 Id 查询级节点
drop function if exists findParent;
CREATE FUNCTION `findParent`(rootId VARCHAR(5)) RETURNS VARCHAR(4000) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);
DECLARE dname VARCHAR(4000);
DECLARE num int;
SET sTemp = '';
SET dname = '';
SET num = 1;
SET sTempChd = rootId ;
WHILE sTempChd != 0 DO
SELECT parent_id,name INTO sTempChd,dname
FROM db_admedia.t_department
WHERE id= sTempChd;
if num = 1 then
SET sTemp = dname;
else
SET sTemp = CONCAT(dname,'|',sTemp);
end if;
set num = num + 1;
END WHILE;
RETURN sTemp;
END;
select findParent('2')
注意:
mysql function 函数传入的值,不能为中文,只能传数值型的
|