1. 程式人生 > >Sql查詢樹狀結構下級子節點的數量

Sql查詢樹狀結構下級子節點的數量

leave SQ 樹狀 div where AR light bsp brush

--查詢Id為1的所有子節點層數的數量
;with cte as ( select id,parentId,0 as Leave from _TestLeave where id = 1  --要查詢的節點 union all select a.id,a.parentId,b.Leave + 1 as Leave from _TestLeave a join cte b on a.parentId = b.id and b.Leave < 6  --層數 ) select leave,count(*) from cte where leave > 0 group by Leave

Sql查詢樹狀結構下級子節點的數量