Mizan
Mizan

Reputation: 460

Parent child relationship in Oracle

I have a parent child relationship stored in a table consider the following example

Id        Name    ParentId
------------------------------
1         Node1      -1
2         Node2      -1
3         Node3       1
4         Node4       1
5         Node5       3
6         Node6       5
7         Node7       2

Here ParentId = -1 means that it is a root level node. Now I want to write an SQL Query which will return child at all level of a parent category.

e.g. for Id = 1, it should return 3, 4, 5, 6.

Parent     Child   
-------------------
1            3    
1            4    
1            5    
1            6    

I am referring this question but it is for sql-server and the output is not as desired.

Upvotes: 2

Views: 7660

Answers (1)

archimede
archimede

Reputation: 256

 select parentid parent, id child
   from table1
connect by prior id = parentid
  start with parentid = 1

Upvotes: 2

Related Questions