Consider the following:
Table - id, parentid
What I’d like to do, is I’d like to pull all the children (not only direct children, but all of them, i.e. children of children of children etc.) of a specific parent.
So let’s say the table contains the following row: (2, 1), (3, 1), (4, 2), (5, 4)
Then for parentid = 1, the table would return ids 2, 3, 4 AND 5.
Is this possible?
If not (and I guess it’s indeed not possible), what are my options?
I really don’t want to use dozens of queries…
P.S. I can’t change the database structure.
Also, as there might be hundreds of thousands of records in the table, I can pull them all and do the whole thing using PHP instead.
This might help:
You may as well use recursion to do so but I think the above will need fewer iterations.
Hope it helps!
EDIT – I forgot to mention that you can as well implement the same logic in a MySQL stored procedure except that you cant use Arrays. The above example is implemented in PHP as you might have already guessed