根据父级部门id找所有子集:
select dept_id,name,parent_id,order_num,company_id,deptNum,parentDeptNum from (
select @guid idlist,
(select @guid:=group_concat(dept_id separator ',') from sys_dept where find_in_set(parent_id,@guid) and del_flag=0) sub
from sys_dept,(select @guid:={参数id}) vars
where @guid is not null) tl,sys_dept t
where find_in_set(t.dept_id,tl.idlist)
根据子集部门id找所有父级:
SELECT t1.sort,t2.dept_id,t2.parent_id,t2.name FROM
( SELECT
@r AS _id,
( SELECT @r := parent_id FROM sys_dept
WHERE dept_id = _id and del_flag=0 and dept_id != 2971
) AS parent_id,
@s := @s + 1 AS sort
FROM ( SELECT @r := {参数id}, @s := 0 ) temp, sys_dept
) t1
JOIN sys_dept t2 ON t1._id = t2.dept_id and t2.dept_id != 2971
order by t1.sort desc