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

mysql实现递归查询

1.首先创建函数

BEGIN

DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);
SET sTemp = ‘$‘;
SET sTempChd = cast(pID as char);

WHILE sTempChd is not NULL DO
SET sTemp = CONCAT(sTemp,‘,‘,sTempChd);
SELECT group_concat(CMNT_ID) INTO sTempChd FROM file_cmnt_lst WHERE FIND_IN_SET(P_CMNT_ID,sTempChd) > 0;
END WHILE;
return sTemp;
END

参数`pID` int

返回类型varchar(4000)

2.查询

 

select * from file_cmnt_lst where FIND_IN_SET(cmnt_id, GetCmtTree(0)); 

 

mysql实现递归查询