-2

Suppose I have table called tree with id, and parent-id and data are b-tree

           1
    2             3
4   5   6      7   8  9

etc

all these are store in tree table like

1 - null
2 - 1
3 - 1
4 - 2
5 - 2
6 - 2
7 - 3
8 - 3
9 - 3

etc

so please write Query to fetch child tree of 2

output should be like

4 - 2
5 - 2
6 - 2
xx - 4
etc..
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78

1 Answers1

0

Select id, parent_id from tablename where parent_id = 2. Perhaps you need bottom level ones? Then you need to left join the table with itself and only return the rows that don't have children.

Edit: if the maximum number of hierarchy levels is high or unknown, you should use hierarchyid: https://msdn.microsoft.com/en-us/library/bb677213.aspx

Hope this helps!

Mihai Ovidiu Drăgoi
  • 1,307
  • 1
  • 10
  • 16