1.表结构

  MySQL 递归查询 当前节点及子节点_表结构

2.查询语句

MySQL 递归查询 当前节点及子节点_查询语句_02
    
create procedure Pro_GetUnderOrg( in idd varchar ( 36 ))
begin
declare 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() > 0
do
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 ;
MySQL 递归查询 当前节点及子节点_查询语句_02

3,调用

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

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