Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting a node in the nested set model through a not-unique name
    primarykey
    data
    text
    <p>I am trying to manage the retrieval of a node in a <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow">nested set model</a> table, not through the unique ID, but <strong>through the name</strong> (a string), and other nodes within the tree under different parents may be called the same way.</p> <p>As far as now I used an unique ID to get nodes inside the nested sets:</p> <pre><code>SELECT node.name, node.lft, node.rgt FROM tbl AS parent, tbl AS node WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.id = '{$node_id}' GROUP BY node.id</code></pre> <p>Trying to extend this method to a more general way to retrieve the node through its name, I came up with a query containing as much HAVING clauses as the depth of the node to retrieve, checking for the node name and its depth:</p> <pre><code>SELECT node.name, node.lft, node.rgt, COUNT(node.id) AS depth FROM tbl AS parent, tbl AS node WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.id HAVING (node.name = 'myParentName' AND depth = 1) OR (node.name = 'myParent2Name' AND depth = 2) OR ... # and so on </code></pre> <p>But it is not perfect: having two nodes with the same name and the same depth, but within different parents, both are retrieved, no matter the hierarchy they belong to.</p> <p>Example:</p> <pre> ARTICLES | +--PHP | +--the-origins | +--syntax +--JS +--history +--syntax </pre> <p>In this case, the query above would return either ARTICLES/PHP/syntax or ARTICLES/JS/syntax: a "syntax" node with depth 3, infact, is either under the PHP node or under the JS node. Is there an effective path to walk, to solve this problem?</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.
 

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