SQL 递归查询(根据指定的节点向上获取所有父节点,向下获取所有子节点)

时间:2025-04-04 13:15:49
WITH TEMP AS 
(
SELECT * FROM t_sys_org WHERE ID='0'   --表的ID
UNION ALL 
SELECT T0.* FROM TEMP,t_sys_org T0 WHERE =T0.parent_id    --子级ID==父级ID
)
SELECT * FROM TEMP;




WITH TEMP AS 
(
SELECT * FROM t_sys_org WHERE ID='91a9c11e-346c-49ef-93c7-d0069d33c6f3'    --表的ID
UNION ALL 
SELECT T0.* FROM TEMP,t_sys_org T0 WHERE TEMP.parent_id=    --父级ID==子级ID
)
SELECT * FROM TEMP;