给你个例子CREATE FUNCTION topoFolderFun(folderIdTmp bigint(20)) RETURNS varchar(1000) 
DETERMINISTIC
begin
declare sTemp varchar(1000);
declare sTempChd varchar(1000);
set sTemp = '';
set sTempChd = cast(folderIdTmp as char);
lable : loop 
set sTemp = concat(sTemp,',',sTempChd);
select group_concat(folderId) into sTempChd from topofolder where find_in_set(superiorId, sTempChd) > 0;
if found_rows()=0 or sTempChd is null then
return substring(sTemp,2);
end if;
end loop;
end$$

解决方案 »

  1.   

    http://blog.csdn.net/acmain_chm/article/details/4142971
    MySQL中进行树状所有子节点的查询
    在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点。但很遗憾,在MySQL的目前版本中还没有对应的功能。 在MySQL中如果是有限的层次,比如我们事先如果可以确定这个树的最大深度是4, 那么所有节点为根的树的深度均不会超过4,则我们可以直接通过left join 来实现。 但很多时候我们..