I'm trying to display a tree structure in MYSQL with this reference (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) and I'm seeing that only works with 1 parent in same table.
If you have another table to join with, nothing go well.
Example:
Table Menu:
+----+--------+-------+
| id | name | order |
| 1 | Father | 0 |
| 2 | Father | 1 |
| 3 | Son | 0 |
| 4 | Child | 1 |
| 5 | Granson| 2 |
+----+--------+-------+
Table Relations
+----+---------+-----------+
| id | menu_id | parent_id |
| 1 | 1 | NULL |
| 2 | 2 | NULL |
| 3 | 3 | 1 |
| 4 | 4 | 3 |
| 5 | 5 | 4 |
+----+---------+-----------+
Do the SELECT
SELECT child_menu.*, menu.* FROM menu, relations AS child_menu
WHERE menu.id = child_menu.menu_id
GROUP BY menu_id
I Have this:
+----+--------+-------+
| id | name | order |
| 1 | Father | 0 |
| 2 | Father | 1 |
| 3 | Son | 0 |
| 4 | Child | 1 |
| 5 | Granson| 2 |
+----+--------+-------+
I'm trying to leave their children in order in the same SELECT.
From what I see in the examples, only works if the parent is in the same table.
Can someone help me?
thank you
Edited: EXPECTED OUTPUT:
+----+--------+-------+
| id | name | order |
| 1 | Father | 0 |
| 3 | Son | 0 |
| 4 | Child | 1 |
| 5 | Granson| 2 |
| 2 | Father | 1 |
+----+--------+-------+
I.E. Father
Son
Child
Grandson
See Question&Answers more detail:os