sql server - Adjacent List - How to retrieve all sub nodes of a given parent by id? -


i have table so

id          parent          title  1           null            cars 2           1               ford 3           2               hatchback 4           2               saloon 5           3               fiesta 6           4               mondeo   

i need query return list of sub nodes specfic id.

for example, if want subnodes of id 2, get

3           2               hatchback 4           2               saloon 5           3               fiesta 6           4               mondeo   

if wanted subnodes of id 3, get

5           3               fiesta 

a word of warning, tree can many levels deep, not few levels have shown above of simplification. presume query need recursive in way?

you should use recursive query:

with t1 (  select t.* t parent=2    union    select t.* t       join t1 on (t.parent=t1.id) ) select * t1 

sqlfiddle demo


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -