Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Tree ordered by parent and child
    primarykey
    data
    text
    <p>I'm trying to display a tree structure in MYSQL with this reference (<a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow">http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/</a>) and I'm seeing that only works with 1 parent in same table.<br> If you have another table to join with, nothing go well. Example:</p> <p>Table Menu:</p> <pre><code>+----+--------+-------+ | id | name | order | | 1 | Father | 0 | | 2 | Father | 1 | | 3 | Son | 0 | | 4 | Child | 1 | | 5 | Granson| 2 | +----+--------+-------+ </code></pre> <p>Table Relations</p> <pre><code>+----+---------+-----------+ | id | menu_id | parent_id | | 1 | 1 | NULL | | 2 | 2 | NULL | | 3 | 3 | 1 | | 4 | 4 | 3 | | 5 | 5 | 4 | +----+---------+-----------+ </code></pre> <p>Do the SELECT</p> <pre><code>SELECT child_menu.*, menu.* FROM menu, relations AS child_menu WHERE menu.id = child_menu.menu_id GROUP BY menu_id </code></pre> <p>I Have this:</p> <pre><code>+----+--------+-------+ | id | name | order | | 1 | Father | 0 | | 2 | Father | 1 | | 3 | Son | 0 | | 4 | Child | 1 | | 5 | Granson| 2 | +----+--------+-------+ </code></pre> <p>I'm trying to leave their children in order in the same SELECT.<br> From what I see in the examples, only works if the parent is in the same table.<br> Can someone help me? thank you</p> <p><strong>Edited:</strong> EXPECTED OUTPUT:</p> <pre><code>+----+--------+-------+ | id | name | order | | 1 | Father | 0 | | 3 | Son | 0 | | 4 | Child | 1 | | 5 | Granson| 2 | | 2 | Father | 1 | +----+--------+-------+ I.E. Father Son Child Grandson </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload