1

i would like to know how can i get all indirect parents of a child in a MySQL database.

My tabel looks like:

 uid | referral_uid | created
 9   | 8            | <timestamp>
 10  | 9            | <timestamp>
 11  | 9            | <timestamp>
 12  | 10           | <timestamp>

So for example for uid=12 i would like to get the indirect referral_uids which are 8 and 9. For uid=11 the indirect parent would be only 8.

Can this be achieved in one sql query? Thank you

I have tried the following http://sqlfiddle.com/#!2/d30b4/8 using the mentioned link for recursive query. I am doing something wrong but i can't really figure it out what.

Pred
  • 8,789
  • 3
  • 26
  • 46
Ionut Bogdan
  • 249
  • 1
  • 3
  • 15
  • possible duplicate of [How to do the Recursive SELECT query in Mysql?](http://stackoverflow.com/questions/16513418/how-to-do-the-recursive-select-query-in-mysql) – default locale Feb 04 '15 at 09:46
  • I have tried this but it doesnt seem to work, and i'm not sure what im doing wrong. http://sqlfiddle.com/#!2/d30b4/8 – Ionut Bogdan Feb 04 '15 at 10:08
  • 1
    Accepted answer assumes that table is ordered by `col1`. Check out [the second answer](http://stackoverflow.com/a/24901882/451518). It seems to work for me: http://sqlfiddle.com/#!2/d30b4/10 – default locale Feb 04 '15 at 10:12
  • Check this out: http://guilhembichot.blogspot.co.uk/2013/11/with-recursive-and-mysql.html – Pred Feb 04 '15 at 10:19

0 Answers0