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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:43:47+00:00 2026-05-27T06:43:47+00:00

I have hierarchical ordered data saved using the modified preorder tree traversal algorithm. Here’s

  • 0

I have hierarchical ordered data saved using the modified preorder tree traversal algorithm.

Here’s tables content:

id  lft  rgt  name
1   1    10   topnode
2   2    3    level1
3   4    7    level1
4   5    6    level2
5   8    9    level1

Visualised:

Scheme

What I want is to select just the childnodes of a certain node (so not the childnodes of the childnodes). Let’s say ‘topnode’. I’m trying to fix a query, but I can’t seem to get my head around it.

Searching the internet brings me a while, for example: I can calculate the depth of each node, but I just can’t seem to select on it.

This query

SELECT node.*, (COUNT(parent.id) - 1) AS depth
FROM tree AS node
CROSS JOIN tree AS parent
WHERE (node.lft BETWEEN parent.lft AND parent.rgt)
GROUP BY node.id
ORDER BY node.lft

shows the depth of each node:

id  lft  rgt  name     depth
1   1    10   topnode  0
2   2    3    level1   1
3   4    7    level1   1
4   5    6    level2   2
5   8    9    level1   1

That’s great, but I can’t use the column depth as a condition!

  • 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-27T06:43:47+00:00Added an answer on May 27, 2026 at 6:43 am

    I think this should do the trick

    SELECT node.*, (COUNT(parent.id) - 1) AS depth
    FROM tree AS node
    CROSS JOIN tree AS parent
    WHERE (node.lft BETWEEN parent.lft AND parent.rgt)
    GROUP BY node.id
    HAVING depth = {{ENTER YOUR REQUIRED DEPTH HERE}}
    ORDER BY node.lft
    

    Hope that helps

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

Sidebar

Related Questions

I have an issue. I have hierarchical XML data such as: <Tree> <Node Text=Stuff
I have hierarchical data stored in the datastore using a model which looks like
I have some hierarchical data - each entry has an id and a (nullable)
I have a hierarchical data structure which I'm displaying in a webpage as a
I have a hierarchical UL list. How to - using jQuery - get the
I have some hierarchical data that I need display as a table, can I
I have a hierarchical data that goes like this: +----------------------+-------+ | name | depth
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I have some hierarchical data that I need to display in a series of
I have a hierarchical relationship defined for one of my tables where the relationship

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.