Get resulting parent/child in a single query...?
Posted: Fri Mar 24, 2006 7:59 pm
I have a parent/child relationship in my table I'm trying to get all parents from a child in a single query. For example: The following table:
If I select by 25 I would like the resulting to be:
24 being a parent of 25, 22 being a parent of 24 and 22 not having a parent.
I'm assuming this is going to be an inner join I'm just not sure how to recursively do it with mysql.
I can create a loop w/ php to do this however; I'd like to see if this can be done in a single query.
Code: Select all
child_id | parent_id
22 | 0
23 | 0
24 | 22
25 | 24
26 | 0
27 | 26Code: Select all
25
24
22I'm assuming this is going to be an inner join I'm just not sure how to recursively do it with mysql.
I can create a loop w/ php to do this however; I'd like to see if this can be done in a single query.