首页 > 代码库 > mysql递归查询

mysql递归查询

1.创建函数

BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);

SET sTemp = ‘$‘;
SET sTempChd = cast(areaId as char);

WHILE sTempChd is not NULL DO
SET sTemp = CONCAT(sTemp,‘,‘,sTempChd);
SELECT group_concat(id) INTO sTempChd FROM organizing where FIND_IN_SET(parentId,sTempChd)>0;
END WHILE;
return sTemp;
END

2.调用方法SELECT id FROM organizing WHERE FIND_IN_SET(id, queryChild(2))

mysql递归查询