I have a MySQL table which is as follows:
id | name | parent_id |
---|
19 | category1 | 0 |
20 | category2 | 19 |
21 | category3 | 20 |
22 | category4 | 21 |
... | ... | ... |
Now, I want to have a single MySQL query to which I simply supply the id [for instance say id=19
] then I should get all its child ids [i.e. result should have ids '20,21,22']....
The hierarchy of the children is not known; it can vary....
I know how to do it using a for
loop... but how to achieve the same using a single MySQL query?