I am trying to come up with a SQL query to read recursive data as below :
ParentId ChildId
1 2
2 3
3 4
4 5
4 6
8 9
If a ParentId
is provided, then the query should return all ChildId
s in a recursive manner.
Example : parentId =1
, result = {2, 3, 4, 5, 6}
I found that connect_by_root
and connect by prior
is what I should be probably using it. However I am not able to get it right.
Database : Oracle and Db2
This should do the trick:
SELECT ChildId, ParentId, LEVEL
FROM myTable
CONNECT BY PRIOR ChildId = ParentId;
This did it :
SELECT childid
FROM btab
START WITH parentId = 1
CONNECT BY PRIOR childId = parentId;
Thanks to #Bulat & #AngocA, for the pointers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.