CREATE FUNCTION `queryChildLst`(rootId VARCHAR(30))
RETURNS varchar(1000)
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = '$';
SET sTempChd =cast(rootId as CHAR);
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(ID) INTO sTempChd FROM T_F_BRANCH where FIND_IN_SET(PID,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
java代码中调用函数方法(传递参数要遍历的树节点)
select a.ID from T_F_BRANCH a where FIND_IN_SET(ID, getChildLst('1'));