mysql function 查询子级机构

DROP FUNCTION IF EXISTS queryChildOrgList;
CREATE FUNCTION queryChildOrgList(id VARCHAR(20))
RETURNS VARCHAR(2000)
BEGIN
DECLARE sTemp VARCHAR(2000) DEFAULT '';
DECLARE displayIds VARCHAR(1000) DEFAULT '';
DECLARE currParentIds VARCHAR(1000) DEFAULT '';
SET sTemp=id;
SET currParentIds=id;
SET displayIds='';
WHILE displayIds IS NOT NULL DO
SET sTemp = CONCAT(sTemp,',',displayIds);
select GROUP_CONCAT(display_id) into displayIds from agent where FIND_IN_SET(parent_id,currParentIds)>0;
SET currParentIds = displayIds;
END WHILE;
RETURN sTemp;
END;
posted @ 2018-05-25 14:19  梦见舟  阅读(262)  评论(0编辑  收藏  举报