LMSouq
database Open

How to create a MySQL hierarchical recursive query?

TA
Tarun Parswani
1 month ago
3 views
Problem Description
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?

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base