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 659091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:00:22+00:00 2026-05-13T23:00:22+00:00

I’m working with a tree structure in MySQL that is respresented using the nested

  • 0

I’m working with a tree structure in MySQL that is respresented using the nested sets model.

I’m hoping some of you sql experts can help me with building a SELECT query.

I would like to be able to match a set of nodes using LIKE. For each node that is matched, I also need a comma-delimmited list of the ancestors of that node, and a comma-delimmited list of the immediate children of that node.

I’m not really sure where to start with this – if such a thing is even possible in a single query. (Currently I am accomplishing this with a query inside a loop.) What I’m hoping for is a result set that might look something like this….

Starting with the string “qu” and querying the Table “Body” I get…

Node      | Parent Nodes               | Immediate Children
Quads       Leg, Lower Body, Muslces     Vastus Lateralus, Vastus Medialis, Rectus Femoris
Obliques    Core, Trunk, Muscles         Inner obliques, outer obliques

Any suggestions on how to accomplish this without looping queries would be much appreciated.

  • 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-13T23:00:22+00:00Added an answer on May 13, 2026 at 11:00 pm

    While I agree with nickf that this is bad and dirty, it’s still fun, so here goes:

    SELECT     base.left_id, base.ancestors, 
               GROUP_CONCAT(children.left_id) children
    FROM       (
                SELECT     base.left_id
                ,          GROUP_CONCAT(ancestors.left_id) ancestors
                FROM       nested_set   base
                LEFT JOIN  nested_set   ancestors
                ON         base.left_id     BETWEEN ancestors.left_id 
                                                AND ancestors.right_id
                WHERE      base.name  LIKE '%criteria%'
                GROUP BY   base.left_id
               ) base                                    
    LEFT JOIN  nested_set   children
    ON         children.left_id BETWEEN base.left_id 
                                    AND base.right_id
    LEFT JOIN  nested_set   inbetween
    ON         inbetween.left_id BETWEEN base.left_id 
                                    AND base.right_id
    AND        children.left_id  BETWEEN inbetween.left_id 
                                    AND inbetween.right_id     
    WHERE      inbetween.left_id IS NULL
    GROUP BY   base.left_id
    

    Basically, the trick is to solve it in two steps: first, solve the ancestors problem, and squash the ancestors to a list with, then, use this result to solve it for the children.

    The ancestors part is relatively easy, it is the subquery in the from clause in my solution.
    The children is a bit harder. It works by taking all descendents, and then requiring that there do not exist any nodes between the base node and the descendents, which basically restricts the descendents to only the children.

    There are other variations to this strategy to solve this – for example you can do the children first, and solve the ancestors using a subquery in the SELECT list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer C++ only comes in play when you work on large… May 15, 2026 at 4:11 am
  • Editorial Team
    Editorial Team added an answer From Using Triggers: Detecting the DML Operation That Fired a… May 15, 2026 at 4:11 am
  • Editorial Team
    Editorial Team added an answer Well, clearly no one in this space can help me.… May 15, 2026 at 4:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.