我有一个简单的 SQL 查询来计算部门中的所有员工(在孩子中),例如:
With Temp(id) AS
(
Select d.id From DEPARTMENT d
Where d.id = 1
UNION ALL
Select d.id From DEPARTMENT d JOIN Temp te ON d.idDepartment = te.id
)
Select count(*) From
(
Select e.id From Employee e Join Temp te On e.idDepartment = te.id
)
但是我给出一个错误“StackOverflow”,我不知道哪里出错了,你能帮我吗?测试用例有一些数据: 表部门:
ID----------departmentName-----------idDepartment(id parent)
1 A 0
2 B 1
表员工:
id----------employeeName------------idDepartment
1 E_1 1
2 E_2 1
3 E_3 2
因此,当我在部门(A)中选择员工数量时--> 结果:3,如果 B 部门--> 结果:1 谢谢!