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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:12:29+00:00 2026-05-23T07:12:29+00:00

I have a column id , a column parent and a column path that

  • 0

I have a column id, a column parent and a column path that is a materialized path.

It looks like

1  | \N | 1  
2  | 1  | 1/2  
3  | 2  | 1/2/3  
4  | 3  | 1/2/3/4  
5  | 3  | 1/2/3/5  
6  | 2  | 1/2/6  
7  | 6  | 1/2/6/7  
8  | 2  | 1/2/8  
9  | 1  | 1/9  
10 | 9  | 1/9/10  
11 | 10 | 1/9/10/11  
12 | 11 | 1/9/10/11/12  
13 | 11 | 1/9/10/11/13  
14 | 11 | 1/9/10/11/14  
15 | 14 | 1/9/10/11/14/15  
16 | 14 | 1/9/10/11/14/16  
17 | 14 | 1/9/10/11/14/17  
18 | 10 | 1/9/10/18  
19 | \N | 19  
20 | 19 | 19\20  
21 | 19 | 19\21

I need to do some queries based off this table.

The queries I need to do are


Select all children of id 9

SELECT * FROM `tester` WHERE 'path' LIKE '%/9/%';  

Would work fine, Until you replace the ID with 1 or 19 as there is no / at the beginning.

SELECT * FROM `tester` WHERE 'path' LIKE '%1/%';

would select all rows where a number ends in 1, so, 1, 11, 21, 31, 211 etc

SELECT * FROM `tester` WHERE 'path' LIKE '1/%';

would work correctly for either rows 1 or 19

So SELECT * FROMtesterWHERE 'path' LIKE '1/%' OR 'path' LIKE '%/1/%';
Is the best I can come up with, any suggestions?


Select Direct children of 9 but not sub-children
For this Select * fromtesterwhere 'parent' = 9; will work fine.


select an aggregate count of 9’s children, x levels deep.

So I want to end up with either one row of level1, level2, level3, ... levelx or x rows, representing the different levels,

Let us pretend x is 3 for this example
The rows from this example would be 9, 8, 6 (the 4th level if we requested it would be 3)

Any Ideas?

Edit

#select count of children of specific node(5) down to a maximum of three levels, do no include the parent
SELECT COUNT(child.id) children, 
LENGTH(REPLACE(child.path, parent.path, '')) - LENGTH(REPLACE(REPLACE(child.path, parent.path, ''), '/', '')) AS LEVEL
FROM `tester` child JOIN `tester` parent ON child.path LIKE CONCAT(parent.path,'%') 
WHERE parent.id  =5 
GROUP BY LEVEL HAVING LEVEL <= 3 AND LEVEL > 0;

**select 9’s children’s id’s down to x levels, with the level relative to 9,

So again for this example we will use 3 as x.

We are looking to get back

10 | 1
11 | 2
18 | 2
12 | 3
13 | 3
14 | 3 

Again I am at a complete loss as to how to do this.

Edit:

#select all information, and relative level from parent of children of specific node(5) down to a maximum of three levels, do no include the parent
SELECT child.*, 
LENGTH(REPLACE(child.path, parent.path, '')) - LENGTH(REPLACE(REPLACE(child.path, parent.path, ''), '/', '')) AS LEVEL
FROM `tester` child JOIN `tester` parent ON child.path LIKE CONCAT(parent.path,'%') 
WHERE parent.id  =9 
GROUP BY id HAVING LEVEL <= 3 AND LEVEL > 0;
  • 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-23T07:12:30+00:00Added an answer on May 23, 2026 at 7:12 am

    Just to give you a heads up, these solutions are based on string comparisons, are not optimized & cannot use indexes. you should consider normalizing your tables differently. (See Managing Hierarchical Data in MySQL)

    Regarding some of the questions:


    Select all children of id 9:

    Since the Path column does not include the leading & trailing slashes, you need to concatenate them to the path:

    SELECT * 
    FROM tester
    WHERE CONCAT('/', path, '/') LIKE '%/9/%';
    

    select an aggregate count of 9’s children, x levels deep:

    We need to group by the number of slashes in the path, minus the number of slashes in the parent path:

    SELECT (LENGTH(c.Path) - LENGTH(REPLACE(c.Path, '/', '')))
        - (LENGTH(p.Path) - LENGTH(REPLACE(p.Path, '/', ''))) AS Level,
        COUNT(*)
    FROM tester c
        JOIN tester p ON c.Parent = p.ID
    WHERE CONCAT('/', path, '/') LIKE '%/9/%';
    GROUP BY 1
    

    For simplicity i used the query above to show all the levels, If you want to limit x levels deep, use the WHERE predicate from the query below.


    select 9’s children’s id’s down to x levels, with the level relative to 9:

    We search the Path column up to a x number of levels, while taking the parents level into consideration:

    SELECT c.*
    FROM tester c
        JOIN tester p ON c.Parent = p.ID
    WHERE CONCAT(
        '/',
        SUBSTRING_INDEX(
            Path, 
            '/', 
            (LENGTH(p.Path) - LENGTH(REPLACE(p.Path, '/', ''))) + 4
        ),
    '/') LIKE '%/9/%'
    

    The steps we are taking:

    1. We need to find out how deep the parent is, we can find that by counting the slashes in the parent’s path. (LENGTH(p.Path) - LENGTH(REPLACE(p.Path, '/', '')))
    2. We need to add 1 to that number, since a path with 1 slash is 2 levels deep.
    3. We add the x number of desired levels.
    4. Grab the path column up to the level total, (Use the SUBSTRING_INDEX function).
    5. Add the leading and trailing slash.
    6. Search the final string for 9.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have column that contains strings. The strings in that column look like this:
I need a select from table which does not have column that tells when
I have a data grid that looks like this <tk:DataGrid ItemsSource={Binding Parents} AutoGenerateColumns=False> <tk:DataGrid.Columns>
I have a table that I select data from with a column called parent
I have a parent-/child relationship of folders, which looks like this: A folder can
I have a table with a column that needs the data type upgrading. However
I have a script that appends some rows to a table. One of the
Let's say I have a Parent object like this (in pseudo code) public Parent()
I got a pretty simple parent/child relationship here, which looks like this: Email servers
I have a column of data that contains a percentage range as a string

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.