话不多说,直接上SQL:
select dept_id,dept_name from (
select
t1.dept_id,
t1.dept_name,
if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0) as ischild
from (
select
dept_id,
parent_id,
dept_name
from ls_cendev.sys_dept t order by parent_id, dept_id
) t1,
(select @pids := '112') t2
) t3 where ischild != 0 OR dept_id = '112'
查询结果:
说明:
- “@pids := '112'”,是你要查询的部门,如查询委办局下所有部门,则条件就是‘112’。
- 若查询部门下所有部门去掉sql最后的“OR dept_id = '112'”条件。
- 若查询本部门,以及本部门下所有部门就要加上“OR dept_id = '112'”条件。
- 若只查询dept_id或dept_name,则根据需要查询即可,该sql都可拿到。