Question

I have a db structure as in the image attached, where in column A I have fathers and in column B sons. As you see a son can also be a father. Now, I would like to know if there is a way through a single query mysql to select all the infinite hierarchical tree.

Thank you.

Father/Sons db structure

Was it helpful?

Solution

There is not a known way of doing this in MySQL with infinite levels. You could always use n self-joins, but using PHP is probably a better approach.

You can however restructure your db schema, for example using Pre Order Tree Traversal:

Storing Hierarchical Data in a Database

This will increase the complexity and runtime of inserting data, but will make it much easier to fetch a tree or subtree.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top