Mysql 递归查询

先看表结构:




看sql语句:

DROP FUNCTION IF EXISTS Fun_RecursionDict;
CREATE FUNCTION `Fun_RecursionDict` (id INT)
RETURNS VARCHAR(4000)
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);

SET sTemp = '$';
SET sTempChd = cast(id as char);

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

#query
select * from pd_dict where FIND_IN_SET(DictID, Fun_RecursionDict(3));



查询结果:

posted @ 2017-11-23 10:11  正怒月神  阅读(323)  评论(0编辑  收藏  举报