2

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.

Community
  • 1
  • 1
Mizan
  • 450
  • 1
  • 5
  • 28

1 Answers1

2
 select parentid parent, id child
   from table1
connect by prior id = parentid
  start with parentid = 1
archimede
  • 256
  • 1
  • 2