1. 根据特定节点,获取其下属的所有子级节点
WITH temp AS ( SELECT * FROM [表名] WHERE Id=9999 --主键Id=9999 UNION ALL SELECT t.* FROM temp, [表名] AS t WHERE temp.Id=t.ParentId --子级Id=父级Id ) SELECT * FROM temp;
2. 根据特定节点,获取其所有父级节点
WITH temp AS ( SELECT * FROM [表名] WHERE Id=9999 --主键Id=9999 UNION ALL SELECT t.* FROM temp, [表名] AS t WHERE temp.ParentId=t.Id --父级Id=子级Id ) SELECT * FROM temp;