drop FUNCTION if exists test;
delimiter //
CREATE FUNCTION `test`(`rid` int) RETURNS varchar(200) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = '';
SET sTempChd = cast(rid as char);
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(id) INTO sTempChd FROM t_organization where FIND_IN_SET(pid,sTempChd)>0 and logic_del=0;
END WHILE;
set sTemp = substring(sTemp,2);
RETURN sTemp;
END;
//
然后具体用法,如下图查询1下级的所有的数据