Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, if you can't change the data model, and you're using MySQL, you're stuck in a situation where you need recursive queries and you're using a DBMS that doesn't support recursive queries.</p> <p>Quassnoi wrote an interesting series of blog articles, showing techniques for querying hierarchical data. His solutions are quite clever, but very complex. <a href="http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/" rel="nofollow noreferrer">http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/</a></p> <p>PostgreSQL is another open-source RDBMS, which does <a href="http://www.postgresql.org/docs/9.2/static/queries-with.html" rel="nofollow noreferrer">support recursive queries</a>, so you could fetch a whole tree stored in the way you show. But if you can't change the data model, I'd assume you can't switch to a different RDBMS.</p> <p>There are several alternative data models that make it much easier to fetch arbitrarily-deep trees:</p> <ul> <li><a href="https://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree/192462#192462">Closure Table</a></li> <li>Nested Sets aka Modified Preorder Tree Traversal</li> <li>Path Enumeration aka Materialized Path</li> </ul> <p>I cover these in my presentation <a href="http://www.slideshare.net/billkarwin/models-for-hierarchical-data" rel="nofollow noreferrer">Models for Hierarchical Data with SQL and PHP</a>, and in my book <a href="http://pragprog.com/book/bksqla/sql-antipatterns" rel="nofollow noreferrer">SQL Antipatterns: Avoiding the Pitfalls of Database Programming</a>.</p> <p>Finally, there's another solution that I've seen used in the code for <a href="http://slashdot.org/" rel="nofollow noreferrer">Slashdot</a>, for their comments hierarchies: They store "parent_id" like in Adjacency List, but they also store a "root_id" column. Every member of a given tree has the same value for root_id, which is the highest ancestor node in its tree. Then it's easy to fetch a whole tree in one query:</p> <pre><code>SELECT * FROM site WHERE root_id = 123; </code></pre> <p>Then your application fetches all the nodes back from the database into an array, and you have to write the code to loop over this array, inserting the nodes into a tree data structure in memory. This is a good solution if you have many separate trees, and each tree has relatively few entries. It's good for Slashdot's case.</p>
    singulars
    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.
    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