在组织机构中,常常遇到查询某部门下所有子类或某部门所有父类的需要,递归方法如下:
1、查询子类集合
SELECT A.*
FROM ORGANIZATIONS A START WITH ID = 97 CONNECT BY NOCYCLE PRIOR A.ID = A.PARENTID ORDER BY A.ID
2、查询父类集合
SELECT A.* FROM ORGANIZATIONS A START WITH ID = 97
CONNECT BY NOCYCLE PRIOR A.PARENTID = A.ID ORDER BY A.ID
-----------------分割线------------
说明:数据库表设计时采用的是所有部门都在同一张表中,子父节点使用parentID外键关联(自己测试时)
参考资料:
a.http://docs.oracle.com/cd/B14117_01/server.101/b10759/queries003.htm#i2060615
b.http://docs.oracle.com/cd/B14117_01/server.101/b10759/pseudocolumns001.htm#i1009261