If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > help with query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-04, 12:04
replicant replicant is offline
Registered User
 
Join Date: Jan 2004
Posts: 1
help with query

Example table:

+--------+-------+
| Parent | Child |
+--------+-------+
| 17 | 16 |
| 18 | 17 |
| 19 | 18 |
| 20 | 19 |
| 21 | 20 |
| 22 | 21 |
+--------+-------+

Now of course the numbers would be allot more random than that, but I have a table of id's that show a parent/child relationship. I am trying to construct a query where I input a id, say 22, and it will return a sequential list of all items that would be related, so in this example it would return:

21
20
19
18
17
16

As 21 is a child of 22, 20 is a child of 21, etc ..

Imagine kinda like a pyramid. Seems there should be an easy solution, I just can't picture it. :-\
Reply With Quote
  #2 (permalink)  
Old 01-27-04, 15:11
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
there isn't

if you want truly unlimited levels up or down, i.e. child to parent to grandparent, etc., or parent to child to grandchild, etc., then you must do this recursively from your scripting language

only oracle, as far as i know, has this built in to the query language

if you can be satisfied with results that are limited to N levels, then you can write a simple N+1 self-join using outer joins

e.g. http://www.sitepoint.com/forums/showthread.php?p=975494
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On