1.表结构
2.查询语句
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 ;
3,调用
4,注意: 这种表设计以后一定要加个 路径字段 以后就不用这么费事了,直接 like 查询了