下面是sqlserver上的语句,我的mysql版本是5.7 不支持这种写法
--递归取当前部门及所有下级部门
WITH DEPT AS
(
select cost_name,department_id,cost_code,parent_cost_code
from 表2
where cost_code in ('T')
UNION ALL
SELECT c.cost_name,c.department_id,c.cost_code,c.parent_cost_code
FROM DEPT d,表2 C
WHERE d.cost_code = c.parent_cost_code
)
SELECT * FROM DEPT ;