dbTalk Databases Forums  

traversing up a tree

comp.databases comp.databases


Discuss traversing up a tree in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
yawnmoth
 
Posts: n/a

Default traversing up a tree - 06-12-2007 , 03:30 PM






Say I have a table with three columns - description, parent_id, and
id. Each parent_id can, itself, point to another id with a
parent_id. It doesn't have to, but it can. If it doesn't, parent_id
will be null.

As such, if you get one id, you can sorta build a list, by looking at
each parent_id's parent_id until parent_id is null.

My question is... is there a way you can get a list of all the
description's with just a single SQL query?

You can get one description by doing this:

SELECT description FROM table WHERE id = $id

You can get two by doing this:

SELECT a.description, b.description FROM table a, table b WHERE a.id =
b.parent_id AND b.id = $id

....and so on and so forth.

The problem with this is that each "depth" has it's own query and
requires you know the "depth" in advance. Also, I don't imagine doing
dozens of implicit joins, like that, is all that efficient.

Is there a way to do this using a single query that doesn't require
you know the "depth" in advance?


Reply With Quote
  #2  
Old   
--CELKO--
 
Posts: n/a

Default Re: traversing up a tree - 06-13-2007 , 10:29 AM






Quote:
Say I have a table with three columns - description, parent_id, and id. Each parent_id can, itself, point to another id with a parent_id. It doesn't have to, but it can. If it doesn't, parent_id
will be null.

1) please learn to post DDL in newsgroup and not your personal
narratives
2) Google up "Nested Sets Model" and use it instead of this adjacency
list model
3) Buy a copy of TREES & HIERARCHIES IN SQL



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.