数据库数据满足树结构时,求一个结点的子结点有哪些

2/22/2017来源:ASP.NET技巧人气:1207

可以先创建函数,然后调用。

drop function if exists queryChildCommentId;
delimiter //
create function queryChildCommentId(parentId int)
returns varchar(4000)
begin
declare ret varchar(4000);
declare retTmp varchar(4000);

set ret = '$';
set retTmp = cast(parentId as char);

while retTmp is not null do
set ret = concat(ret, ',' , retTmp);
select group_concat(comment_parent_id) into retTmp from t_comment where find_in_set(parentId, retTmp) > 0;
end while;
return retTmp;
end//
delimiter ;

select queryChildCommentId(0);