1

私は次のテーブル構造を持っています:

ORGANIZATION_ID   | Name  |  PARENT_ID
--------------------------------------------------
1                 | A     |     0   -Indicates root
2                 | B     |     1
3                 | C     |     2
4                 | D     |     2
5                 | E     |     4
6                 | F     |     1
7                 | G     |     1
8                 | H     |     7
9                 | J     |     8
10                | K     |     9

私はOracleSQLクエリを書くのが苦手です。*特定の組織IDを渡した場合、どのようにしてすべての()子組織のリストを生成できますか?

たとえば、渡した場合2、論理的には、親IDが2のすべての行を検索し、同じことを実行している各行を再帰的に検索します。

ロジックを知っていますが、OracleでSQLクエリを使用してロジックを再作成するにはどうすればよいですか?

4

2 に答える 2

3

CONNECT BYこの再帰クエリを作成するために使用できます。

SELECT organization_id, name
FROM t
CONNECT BY PRIOR organization_id = parent_id
START WITH organization_id = 2
于 2012-06-20T13:46:09.773 に答える
2

Oracle 11gを使用する場合は、Oracle再帰サブクエリが代わりになります。

with orgs (organization_id, name, parent_id) as (
    select organization_id, name, parent_id 
        from organization
        where organization_id=2
    union all
        select organization_id, name, parent_id 
        from organization org join orgs on org.parent_id=orgs.organization_id)
search depth first by organization_id set a
select organization_id, name
from orgs;
于 2012-09-10T15:15:04.870 に答える