获取错误代码:2013在运行递归存储过程时查询期间与MySQL Server的连接丢失
我已经阅读了几乎所有相关的问题。但是它对我不起作用。 我正在尝试通过整个子父母数据进行遍历,以便我进行了递归存储程序。
CREATE DEFINER=`root`@`localhost` PROCEDURE `g_tree`(in p_parent varchar(30), in depth int)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
/*table consisting all child*/
if depth = 0 then
drop table if exists final_child_tree;
create temporary table final_child_tree (final_child varchar(15));
end if;
/*child_t stores all connections of p_parent as single record of set of strings*/
drop temporary table if exists child_t;
create temporary table child_t (t_child varchar(15), iterated int default 0);
/*make table consitsing all conneciton in distinct record*/
set @list := (select child from connections where parent = p_parent );
iterator:
LOOP
select * from child_t;
IF CHAR_LENGTH(TRIM(@list)) = 0 OR @list IS NULL THEN
select * from final_child_tree;
LEAVE iterator;
END IF;
select * from final_child_tree;
SET _next = SUBSTRING_INDEX(@list,',',1);
SET _nextlen = CHAR_LENGTH(_next);
SET _value = TRIM(_next);
INSERT INTO child_t VALUES (_value,0);
insert into final_child_tree values (_value);
SET @list = INSERT(@list,1,_nextlen + 1,'');
END LOOP;
/*get child of child*/
iterator :
loop
if exists (select t_child from child_t where iterated = 0 limit 1) then
set @new_parent := (select t_child from child_t where iterated = 0 limit 1);
set @childExists := (select count(*) from connections where parent = @newParent);
if @new_parent != 0 and @childExists != 0 then
call g_tree (@new_parent,1);
end if;
else
leave iterator;
end if;
end loop;
END
它丢了一个错误:
错误代码:2013在查询期间与MySQL Server丢失了连接
错误代码:2013年在查询表格
:连接:
+--------+----------------------+ |父|孩子| +--------+-----------------+ | 1 | C1,C2,C3 | | C1 | C11,C12,C13 | | C2 | C21,C22 | +--------+-----------------+
还有其他更好的方法吗?
I have read almost all related questions. but its not working for me.
I am trying to traverse through whole child parent data to do so I have made recursive stored procedure.
CREATE DEFINER=`root`@`localhost` PROCEDURE `g_tree`(in p_parent varchar(30), in depth int)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
/*table consisting all child*/
if depth = 0 then
drop table if exists final_child_tree;
create temporary table final_child_tree (final_child varchar(15));
end if;
/*child_t stores all connections of p_parent as single record of set of strings*/
drop temporary table if exists child_t;
create temporary table child_t (t_child varchar(15), iterated int default 0);
/*make table consitsing all conneciton in distinct record*/
set @list := (select child from connections where parent = p_parent );
iterator:
LOOP
select * from child_t;
IF CHAR_LENGTH(TRIM(@list)) = 0 OR @list IS NULL THEN
select * from final_child_tree;
LEAVE iterator;
END IF;
select * from final_child_tree;
SET _next = SUBSTRING_INDEX(@list,',',1);
SET _nextlen = CHAR_LENGTH(_next);
SET _value = TRIM(_next);
INSERT INTO child_t VALUES (_value,0);
insert into final_child_tree values (_value);
SET @list = INSERT(@list,1,_nextlen + 1,'');
END LOOP;
/*get child of child*/
iterator :
loop
if exists (select t_child from child_t where iterated = 0 limit 1) then
set @new_parent := (select t_child from child_t where iterated = 0 limit 1);
set @childExists := (select count(*) from connections where parent = @newParent);
if @new_parent != 0 and @childExists != 0 then
call g_tree (@new_parent,1);
end if;
else
leave iterator;
end if;
end loop;
END
and its throwing an error :
Error Code: 2013 Lost connection to MySQL server during query
tables :
connection :
+--------+-------------+ | parent | child | +--------+-------------+ | 1 | c1,c2,c3 | | c1 | c11,c12,c13 | | c2 | c21,c22 | +--------+-------------+
is there any other better way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存储层次数据的归一化方法是存储对父母的引用,而不是存储孩子的逗号分隔列表。
现在您可以使用递归CTE查询来从树的给定节点中获取所有后代:
The normalized way to store hierarchical data is to store a reference to the parent, not store a comma-separated list of the children.
Now you can use a recursive CTE query to get all the descendants from a given node of the tree: