Home > Database > Mysql Tutorial > MySQL递归查询当前节点及子节点_MySQL

MySQL递归查询当前节点及子节点_MySQL

WBOY
Release: 2016-06-01 13:29:34
Original
1011 people have browsed it

bitsCN.com

MySQL递归查询当前节点及子节点

 

1.表结构

 

MySQL递归查询当前节点及子节点_MySQL

 

2.查询语句

 

create procedure Pro_GetUnderOrg(in idd varchar(36))begindeclare lev int;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 lev=lev+1;     INSERT tmp1 SELECT t.ID,t.`OrgName`,t.`ParentID`,lev from organization t join tmp1 a on t.ParentId=a.ID AND levv=lev-1;end while ;    INSERT tmp1 SELECT ID,OrgName,ParentID,0 FROM `organization` WHERE ID=idd;     SELECT * FROM tmp1;end;
Copy after login

 

 

3,调用

 

call Pro_GetUnderOrg('9B2AE044-3B51-47FA-A24C-8326BA7B9FD3');

4,注意: 这种表设计以后一定要加个 路径字段 以后就不用这么费事了,直接 like 查询了 

bitsCN.com
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template