函数
DROP FUNCTION IF EXISTS GetChildren;
CREATE FUNCTION GetChildren(InputDeptId char(36))
RETURNS VARCHAR(4000)
BEGIN
declare deptIds VARCHAR(4000) default '';
select CONCAT(@pids,',',IFNULL(GROUP_CONCAT(DeptId),'$')) into deptIds from (
select t1.DeptId,
if(find_in_set(UpperId, @pids) > 0,
@pids := concat(@pids, ',', DeptId), 0) as ischild
from (
select DeptId,UpperId from MDM_Dept t
where t.STATUS=1
order by UpperId, DeptId
) t1,
(select @pids := InputDeptId) t2
) t3 where ischild != 0 ;
return deptIds;
END;
调用
select * from MDM_Dept
where FIND_IN_SET(DeptId, GetChildren('b9d22dc7-5651-4e87-a59b-7f11ef178e45'))
select * from
(
select GetFullPath(DeptId) fullPath,DeptId,`Name`,UpperId
from MDM_Dept
where FIND_IN_SET(DeptId, GetChildren('b9d22dc7-5651-4e87-a59b-7f11ef178e45'))
) t order by t.fullPath