How in 1 request to get all branch with childs?

Hello,
Having table with fields id, parent_id, title (mysql5) how can I with 1 request to get all branch with childs
of parent N ? I mean grandchilds and deeper too…

Thanks!

For this you may have to use a recursive function in sql.
If parent_id can be the parents of the entities with id(that’s what I suppose), then you may need to frame it somewhat this way -

WITH func as ( select distinct id as child from tablename where parent_id = N
UNION
select distinct child from tablename, func
where tablename.parent_id = func.child)
select * from func

Please go through this link

Go through the part where it tries to find ancestors of a child in the ParentOf table. I think your problem is the reverse. So you need to find children, grand children, … of a parent.

1 Like