SQL 递归枚举子树
示例
WITH RECURSIVE ManagedByJames(Level, ID, FName, LName) AS (-- start with this row
SELECT 1, ID, FName, LName
FROM Employees
WHERE ID = 1
UNION ALL
-- get employees that have any of the previously selected rows as manager
SELECTManagedByJames.Level+ 1,
Employees.ID,
Employees.FName,
Employees.LName
FROM Employees
JOIN ManagedByJames
ONEmployees.ManagerID= ManagedByJames.ID
ORDER BY 1 DESC -- depth-first search
)
SELECT * FROM ManagedByJames;
水平 | ID | 姓名 | 名称 |
---|---|---|---|
1 | 1 | 詹姆士 | 史密斯 |
2 | 2 | 约翰 | 约翰逊 |
3 | 4 | 约翰松 | 史密斯 |
2 | 3 | 麦可 | 威廉姆斯 |
以上是 SQL 递归枚举子树 的全部内容, 来源链接: utcz.com/z/343158.html