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

  • SEARCH
  • Home
  • 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 8315003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:00:47+00:00 2026-06-08T21:00:47+00:00

We are using a hybrid of the nested sets model to allow a child

  • 0

We are using a hybrid of the nested sets model to allow a child to have multiple parents. It is an expansion on the nested set model described in Mike Hillyers blog: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ – this might help explain some of the column names.

I am fairly new to SQL but I have investigated joins, subqueries etc. and nothing seems to offer me the result I need. I expect the answer is fairly simple though and will almost definitely be a LEFT JOIN but I can’t put my finger on it.

I have a simple table called ‘nested_parts’ with 5 columns: ‘tree_id’, ‘part_id’, ‘lft’, ‘rgt’ and ‘mapping’.

The following query returns the values I want to compare in the second query.

SELECT * FROM nested_parts WHERE part_id = 125

Specifically, I now know which tree_id’s that part#125 exists in and the lft and rgt values for each tree_id.

I now need to know all the part_id’s which are in the array of tree_id results I pulled earlier.

I used this a subquery for this:

SELECT * FROM nested_parts
WHERE tree_id = ANY (SELECT tree_id AS tbl FROM nested_parts WHERE part_id = 125)

Now I need to know which part_id’s have lft and rgt values that are not between the lft and rgt values of part#125 within the scope of each tree_id. This would be easy if I had two tables to compare still, or could use virtual tables but this doesn’t seem possible here without a loop.

Any help gratefully received but please appreciate that I am stupid not lazy, and although I have read plenty about UNIONS, JOINS, HAVING, WHERE, SELECT(SELECT), with the complexity of the data on top, I am finding this extremely confusing.

Kind regards,

James

  • 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-06-08T21:00:49+00:00Added an answer on June 8, 2026 at 9:00 pm

    While I would recommend revisiting your data architecture, I’ll try to help you get this particular problem solved.

    First, let’s revisit JOINs. Your query

    SELECT * FROM nested_parts WHERE tree_id = ANY (SELECT tree_id AS tbl FROM nested_parts WHERE part_id = 125)
    

    can be simplified with a JOIN

    SELECT np.* FROM nested_parts AS np
    INNER JOIN nested_parts AS np2
    ON np.tree_id = np2.tree_id
    AND np2.part_id = 125
    

    I will answer a bit more completely after I finish reading the question and fully understand it. I hope this helps for now.

    UPDATE:

    In order to get ONLY records where the lft and rgt values are not between the lft and rgt values from part #125, you have to use the HAVING condition.

    SELECT np.* FROM nested_parts AS np
    INNER JOIN nested_parts AS np2
    ON np.tree_id = np2.tree_id
    AND np2.part_id = 125
    HAVING np.lft > np2.rgt
        AND np.rgt < np2.lft
    

    If my logic is bad here, let me know. I think this is what you’re looking for, though.

    EDIT:

    Your query can also combine conditions for the join

    SELECT np.* FROM nested_parts AS np
    INNER JOIN nested_parts AS np2
    ON np.tree_id = np2.tree_id
    AND np2.part_id = 125
    AND np.lft > np2.rgt
    AND np.rgt < np2.lft
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Mac software (using DiscRecordingFramework and IOKit) that creates hybrid Video-DVD.
I'm building an hybrid Android App using Phonegap/Apache Cordova. My app should get data
I am developing hybrid application by using webview and native both. I am using
I have a table in my database where I store a tree structure using
I have a large application that currently exists as a hybrid of WebForms and
I have what could be seen as a bizarre hybrid of IQueryable<T> and IList<T>
i mainly use PHP and I have been coding using it for a while.
I am building a hybrid MVC/Webforms application where my MVC views are using an
I have been using Rails for over 4 years so obviously I like Rails
I have a web application that needs to be built using PHP/MySQL. The application

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.