DROP FUNCTION IF EXISTS `WhlFucGetIDsOnID`;

CREATE DEFINER = `root`@`127.0.0.1` FUNCTION `WhlFucGetIDsOnID`(`inID` bigint)
RETURNS text
BEGIN
#Routine body goes here...
DECLARE sTemp TEXT;
DECLARE sTempChd TEXT; SET sTemp = '$';
SET sTempChd =cast(inID as CHAR); WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(jc_whatall.aid) INTO sTempChd FROM jc_whatall where FIND_IN_SET(jc_whatall.af,sTempChd)>0;
END WHILE;
RETURN sTemp;
END;

 以上代码参考:http://www.2cto.com/database/201209/152513.html,请尊重原作者。 

jc_whatall表结构

sql如何做递归层次查询-LMLPHP

04-28 06:40