Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1105863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:42:06+00:00 2026-05-17T01:42:06+00:00

I am trying to manage the retrieval of a node in a nested set

  • 0

I am trying to manage the retrieval of a node in a nested set model table, not through the unique ID, but through the name (a string), and other nodes within the tree under different parents may be called the same way.

As far as now I used an unique ID to get nodes inside the nested sets:

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

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:

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

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.

Example:

ARTICLES
   |
   +--PHP
   |   +--the-origins
   |   +--syntax
   +--JS
       +--history
       +--syntax

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:42:07+00:00Added an answer on May 17, 2026 at 1:42 am

    I’m not quite sure what you’re trying to do here. Are you trying to access the node with the pathname ARTICLES/PHP/syntax? If so what you’d need to do would be a self-join for each parent level:

    SELECT n0.*
    FROM tbl AS n0
    JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
    JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
    WHERE n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'
    

    ETA re comments: doing direct-child matches in nested set isn’t much fun. You’d have to try to select an intermediate parent row between each joined row. This is the row you don’t want to exist, so you then invert that condition with a null left join. eg.:

    SELECT n0.*
    FROM tbl AS n0
    JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
    JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
    LEFT JOIN tbl AS nn1 ON nn1.lft BETWEEN n1.lft+1 AND n0.lft-1
    LEFT JOIN tbl AS nn2 ON nn2.lft BETWEEN n2.lft+1 AND n1.lft-1
    WHERE nn1.lft IS NULL AND nn2.lft IS NULL
    AND n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a desktop application written in C#. It is trying to manage a
I'm trying to use libvirt with virsh to manage my kvm/qemu vms. The problem
I am currently trying to use NAnt and CruiseControl.NET to manage various aspects of
help me to manage IIS (what i am trying to do is create virtual
I've had a hard time trying to find good examples of how to manage
I'm using Apache Subversion to manage and store a decent volume of code. Trying
I am trying to write a small Cocoa application that helps me manage my
I am trying to figure the best way to manage my javascript code for
Not even sure if this is feasible, but here's the use case: I have
I am trying to get process path by pid but I'm getting Win32Exception (access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.