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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:53:55+00:00 2026-05-13T14:53:55+00:00

My first post here! Seems like this is the place to get wise ;)

  • 0

My first post here! Seems like this is the place to get wise 😉

I am currently in the middle of some testing with my first ever attempt to try the MPTT (Modified Preorder Tree Traversal) approach to storing data in my Mysql database with the help of PHP.

However, I am trying to find out the most performance-oriented way to get all list elements on a specific level, with a specific parent.

That could translate into getting the categories Saab and Chrysler from the image below, if the parent entered would be named “Bilar”. (Which means Cars in Swedish, if that isnt your strongest side 😉 )

As i cant post images, here’s a link to the flowchart: http://www.phpsidan.nu/files/mptt/mptt1.png

Currently i have been doing two querys to do what i want, and quite some amount of code to calculate the level and spit out all the other elements on the same level.

Is there a better way to do this and by hopefully only using one query?

Thanks a lot!

  • 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-13T14:53:55+00:00Added an answer on May 13, 2026 at 2:53 pm

    I am sure this could be optimized, however assuming you have the columns “name”, “lft” and “rgt”, the following will give you the level 2 siblings of “Bilar”.

    SELECT node.name,                                                                                                                                     
           node.lft AS sort,                                                                                                                                                                                                                                                               
           (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth                                                                        
    
    FROM car AS node,                                                                                                                                  
         car AS parent,                                                                                                                                
                 car AS sub_parent,                                                                                                                            
                 ( SELECT node.name, (COUNT(parent.name) - 1) AS depth                                                                                          
                     FROM car AS node,                                                                                                                         
                          car AS parent                                                                                                                        
                    WHERE node.lft BETWEEN parent.lft AND parent.rgt                                                                                            
                      AND node.name = "Bilar"                                                                                                                       
                 GROUP BY node.name                                                                                                                             
                 ORDER BY node.lft) AS sub_tree
    
    WHERE node.lft BETWEEN parent.lft AND parent.rgt                                                                                                     
      AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt                                                                                             
      AND sub_parent.name = sub_tree.name
    
    GROUP BY node.name HAVING depth <= 2                                                                                                                 
    ORDER BY node.lft
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here and I wanted to get some input from
So I saw this post here and read it and it seems like bulk
Well, this is my first post here and really enjoying the site. I have
First post here so sorry if I seem like a newb, I am trying
this is first post here and I am a noob programmer. This may be
Hi this is my first post here hope you all are well. So Im
First post, so here goes. I'm writing a script that does intelligent search and
This is my first post and I'm quite a novice on C++ and compiling
This is my first post, but this question has racked my brain and Google's
My first post here - it's a great site and I will certainly do

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.