Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hierarchical query

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Say, I have a "Thing" table that's structure as follows:

+---------+------+-...-+---------------+
| ThingID | Name | ... | ParentThingID |
+---------+------+-...-+---------------+
| | | | |

Where a Thing can be a child of another Thing and on, and on. Say, for instance a Thing named "C" is a child of "B", which is a child of "A". Is there a way to do a single query that will return "A/B/C"?
 
it is easy to do if the number of levels in the hierarchy is fixed

select t1.thingid t1id, t1.name t1name,
t2.thingid t2id, t1.name t2name,
t3.thingid t3id, t1.name t3name
from tablename t1, tablename t2, tablename t3
where
t3.parentid = t2.id and t2.parentid = t1.id

add outer joins if needed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top