Suppose I have a table of objects structured in a hierarchy:
A
|--B
|--C
| +--D
+--E
They are stored in a "parent-child" table thus:
parent child
A B
A C
C D
A E
How do I query this to get the structure defined above? I think I need something that produces info like this:
object full_path
A NULL
B A
C A
D A.C
E A
I cannot figure out how to do the objects nested more than one level deep. It feels like I might need to iterate over the table (no idea if this is possible in SQL), or otherwise use some kind of query I've never encountered before.
Additional Info:
GROUP BY and MIN statement.The other answers are correct, there is no recursive functionality in mysql. This link describes how to approach hierarchies in mysql: http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/
I found the link on two other SO questions: How to do MySQL Looped Join which tests if results are complete? and Hierarchical Data in MySql .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With