Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, there are a few patterns you could use. Which one is right depends on your needs. </p> <p>Do you need to select a node and all its children? If so, then a <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">Nested set Model (Scroll down to the heading)</a> may be better for you. The table would look like this:</p> <pre><code>| Name | Left | Right | | Emails | 1 | 12 | | Business | 2 | 7 | | Tim's | 3 | 4 | | 7-11 | 5 | 6 | | Churches | 8 | 11 | | St. Pete | 9 | 10 | </code></pre> <p>So then, to find anything below a node, just do</p> <pre><code>SELECT name FROM nodes WHERE Left &gt; *yourleftnode* AND Right &lt; *yourrightnode* </code></pre> <p>To find everything above the node:</p> <pre><code>SELECT name FROM nodes WHERE Left &lt; *yourleftnode* AND Right &gt; *yourrightnode* </code></pre> <p>If you only want to query for a specific level, you could do an <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">Adjacency List Model (Scoll down to the heading)</a>:</p> <pre><code>| Id | Name | Parent_Id | | 1 | Email | null | | 2 | Business | 1 | | 3 | Tim's | 2 | </code></pre> <p>To find everything on the same level, just do:</p> <pre><code>SELECT name FROM nodes WHERE parent_id = *yourparentnode* </code></pre> <p>Of course, there's nothing stopping you from doing a hybrid approach which will let you query however you'd like for the query at hand</p> <pre><code>| Id | Name | Parent_Id | Left | Right | Path | | 1 | Email | null | 1 | 6 | / | | 2 | Business | 1 | 2 | 5 | /Email/ | | 3 | Tim's | 2 | 3 | 4 | /Email/Business/ | </code></pre> <p>Really, it's just a matter of your needs...</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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