First of all this question is similar to this one but the difference is that I have an unknown quantity of parents.
Is is possible and if so how would I get all parents of a child when I only know the Id of any child?
I have a table like:
+----+------------+----------+--------+
| Id |    name    | position | parent |
+----+------------+----------+--------+
|  1 |        top |        1 |   NULL |
|  2 |     middle |        1 |      1 |
|  3 | bottom_1_1 |        1 |      2 |
|  4 | bottom_1_2 |        2 |      2 |
|  5 |   middle_2 |        2 |      1 |
|  6 | bottom_2_1 |        1 |      5 |
|  7 | bottom_2_2 |        2 |      5 |
Which would correspond to something like this visually:
top
 Ͱ middle
    Ͱ bottom_1_1
    Ͱ bottom_1_2
 Ͱ middle_2 
    Ͱ bottom_2_1
    Ͱ bottom_2_2
Unfortunately my knowledge of MySQL is not advanced enough to show some kind of foreach join method to give you as an example as I would only be able to go up 1 in the tree like:
SELECT * FROM table WHERE Id in (7, (SELECT parent FROM table WHERE Id = 7))
This would return:
+----+------------+----------+--------+
| Id |    name    | position | parent |
+----+------------+----------+--------+
|  5 |   middle_2 |        2 |      1 |
|  7 | bottom_2_1 |        2 |      5 |
But I would need something like the example below as the first entry of the above example still has a parent
+----+------------+----------+--------+
| Id |    name    | position | parent |
+----+------------+----------+--------+
|  1 |        top |        1 |   NULL |
|  5 |   middle_2 |        2 |      1 |
|  7 | bottom_2_1 |        2 |      5 |
 
    