Recursive query of MySQL stored procedure

Source: Internet
Author: User

Recursive querying of tables stored in a tree structure, using navicat for MySQL to create stored procedures Pro_show_knowledge, Parameters-in ' rootid ' int, querying all child node data through a knowledge point and inserting it into a temporary table Tmp_knowl The Edge_data.

Note the setting of the depth, set max_sp_recursion_depth = 100; This remark must be added.

begin# set the maximum layer depth  set max_sp_recursion_depth = 100; #创建临时表tmp_knowledge_data for recursive queries, Used to store all child node data under a knowledge point     CREATE TEMPORARY TABLE IF NOT EXISTS  Tmp_knowledge_data (' id '  int (one)  NOT NULL,                            ' name '  varchar ( DEFAULT NULL,           )                 ' PId '  int (11)  DEFAULT NULL,                            ' Create_time '  datetime  default null,                            ' Modify_time '  datetime DEFAULT NULL,                    ' ndepth '  int (one)   default null,                    ' Is_parent '  int DEFAULT NULL,                            primary key  (' id '),                           UNIQUE KEY  ' Id_index '   (' id ')  USING BTREE,                           KEY  ' Pid_index '   (' PId ')  using btree)  ENGINE=InnoDB DEFAULT CHARSET=utf8;     DELETE FROM  Tmp_knowledge_data;     call pro_create_childlst (rootId,0);      select * from tmp_knowledge_data; END

Child nodes Determine whether the data is a parent before inserting the temporary table, and save the Isparent property to a temporary table

begin# Start Cycle Declare done int default 0;declare b int;declare cur1 cursor  FOR SELECT id FROM mooc_si_knowledge_tree where pId=rootId and  Delete_flag=0;declare continue handler for not found set done = 1 ;open cur1; fetch cur1 into b;if done =0theninsert into tmp_knowledge_data  (ID, name,pid,create_time,modify_time,ndepth,is_parent)   (Select id,name,pid,create_time,modify_time, Ndepth,1 as is_parent from mooc_si_knowledge_tree where id = rootid);  ELSEinsert into tmp_knowledge_data  (id,name,pid,create_time,modify_time,ndepth,is_parent)   (select id,name,pid,create_time,modify_time,ndepth,0 as is_parent from mooc_si_ Knowledge_tree where id = rootid); end if; While done=0 docall pro_create_chIldlst (b,ndepth+1); fetch cur1 into b; end while;        close cur1; #循环结束END


Recursive query of MySQL stored procedure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.