Oracle cascade query and Oracle cascade Query
There is a method in ORACLE Database for cascading QuerySelect *// The field to be queriedFrom table// Table with child and parent contact IDSStart with selfid = id// Specify a startid (the Field name is the Child Contact ID and the start ID)Connect by prior selfid = parentid// The join condition is that the child contact is equal to the parent contact. This SQL statement is mainly used for Cascade queries. A parent contact can be used to identify all child contacts. And the sub-contacts of the sub-contacts. It is very practical to check the end. For example, in the flight schedule airline, how can I use SQL statements to query all destinations that can be reached from Guangzhou and allow any transit. FLIGHTNO ORIGIN DESTINATION
-------------------------------------------
Cz3001 CAN CSX
Cz3002 CAN SHA
Cz3003 CSX SHA
Cz3004 CSX PEK
Cz3005 SHA XIY
Cz3006 SHA SWA
Cz3007 PEK URC
Cz3008 PVC AMS
Cz3009 WUH PVC
The root of cz3010 wuh xiy is CAN, and the SQL statement is as follows:Select t. destination from airline t start with origin = 'can 'connect by prior destination = origin; Query results:
DESTINATION
-------------------
CSX
SHA
XIY
SWA
PEK
URC
SHA
XIY
SWA
9 rows selected .--------------------------------The following examples can be viewed on the Internet for better understanding:The data structure is as follows:
T1
T11
T111
T1111
T12
T121
T1211
The db data fields are as follows:
Task_id task_name t. parent_task_id ***
************
000001 t1 ******
000002 t11 000001 ***
000005 t12 000001 ***
000003 t111 000002 ***
000004 t1111 000003 ***
000006 t121 000005 ***
000007 t1211 000006 ***
************
Query statement:
Select t. task_id, t. task_name, t. parent_task_id
From t_task t
Start with task_id = '000000'
Connect by prior task_id = parent_task_id; http://roucheng.cnblogs.com/
Result:
Task_id task_name t. parent_task_id
000001 t1
000002 t11 000001
000003 t111 000002
000004 t1111 000003
000005 t12 000001
000006 t121 000005
000007 t1211 000006
Strat with specifies the conditions for starting a hierarchy, that is, the rows that meet this condition can be used as the top layer of the hierarchy tree.
Connect by prior refers to the association condition between layers, that is, what kind of row is the Child row of the upper row (self-join condition)
Select level, id, name, parentid from temptable2
Connect by prior parentid (top-level column) = id (child-layer column) start with id = 1
Http://www.cnblogs.com/roucheng/p/5404594.html