fean
fean

Reputation: 546

query for sequence of child to parent in mysql

i need such a query where child and parent field are given with a relationship, each child has a parent and this parent is a child of other parent and so on. In this query i need a sequence of child to parent until parent is not NULL.

as like if i start from Daniel then the relult should be

| Elec | 
| Flyo | 
| Sad  |

The table parent_child given below,

child      | parent
----------------------
Sad        | NULL
Buddy      | Clone
Clone      | Daniel
Daniel     | Elec
Elec       | Flyo
Flyo       | Sad

Upvotes: 1

Views: 119

Answers (1)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39981

It is not possible with a single query but you can write a recursive function that calls it self until the answer is found.

Upvotes: 1

Related Questions