Recursive query statement in Oracle

xiaoxiao2021-03-05  25

A recursive query is supported in Oracle 9i. When you use itself in your table, you can build a tree structure with a SQL statement, which is very convenient!

Such as:

Table Department

ID parent_id name

1 0 ### Company

2 1 Market Department

3 1 Technical Department

4 3 R & D Department

5 3 Network

6 3 After-sales Service Department

7 1 Finance Department

Simply use the SQL query to construct a tree structure

Select Department.id, RPAD ('', Level * 3-2, ') ||' '|| Department.name from Department Start with Parent_Id =' 0 'Connect by Prior Department.oid = Department.parent_ID

The results of the query are as follows:

1 | - ### Company

2 | - Marketing Department

3 | - Technical Department

4 | - R & D department

5 | - Network Department

6 | - After-sales service department

7 | - Finance Department

It is very convenient to recurrent query sub-nodes in its own relational table, and the condition can be recursively query the parent node.

转载请注明原文地址:https://www.9cbs.com/read-35424.html

New Post(0)