MySQL recursively queries the current node and Its subnodes

Source: Internet
Author: User

MySQL recursively queries the current node and subnode 1. Table Structure 2. query statements

Create procedure Pro_GetUnderOrg (in idd varchar (36) begindeclare levint; set lev= 1; drop table if exists tmp1; create table tmp1 (id varchar (36 ), orgName varchar (50), ParentID varchar (36), levv INT); INSERT tmp1 select id, OrgName, ParentID, 1 FROM 'organization' WHERE ParentID = idd; while row_count ()> 0do set maid; INSERT tmp1 SELECT t. ID, t. 'orgname', t. 'parentid', levfrom organization t join tmp1 a on t. parentId =. id and levv = lev-1; end while; INSERT tmp1 select id, OrgName, ParentID, 0 FROM 'organization' where id = idd; SELECT * FROM tmp1; end;

 

3. call Pro_GetUnderOrg ('9b2ae044-3B51-47FA-A24C-8326BA7B9FD3 '); 4. Note: After designing this table, you must add a path field so that you don't have to worry about it.

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.