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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:17:53+00:00 2026-05-31T22:17:53+00:00

I have a tabel containing a column named parent which is able to store

  • 0

I have a tabel containing a column named parent which is able to store a parent ID.
This makes it possible to create a tree-structure of my data.

Are there any good helper-functions for travelling through such a tree-structure in MySQL?

For example, if I have a row in my table and I want to be able to retreive all “parents” above it. So getting the it’s parent’s parent ID and so on…

  • 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-31T22:17:55+00:00Added an answer on May 31, 2026 at 10:17 pm

    Copied from this popular link:

    CREATE TABLE nested_category (
            category_id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(20) NOT NULL,
            lft INT NOT NULL,
            rgt INT NOT NULL
    );
    
    INSERT INTO nested_category VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
     (4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
     (9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);
    
    SELECT * FROM nested_category ORDER BY category_id;
    
    +-------------+----------------------+-----+-----+
    | category_id | name                 | lft | rgt |
    +-------------+----------------------+-----+-----+
    |           1 | ELECTRONICS          |   1 |  20 |
    |           2 | TELEVISIONS          |   2 |   9 |
    |           3 | TUBE                 |   3 |   4 |
    |           4 | LCD                  |   5 |   6 |
    |           5 | PLASMA               |   7 |   8 |
    |           6 | PORTABLE ELECTRONICS |  10 |  19 |
    |           7 | MP3 PLAYERS          |  11 |  14 |
    |           8 | FLASH                |  12 |  13 |
    |           9 | CD PLAYERS           |  15 |  16 |
    |          10 | 2 WAY RADIOS         |  17 |  18 |
    +-------------+----------------------+-----+-----+
    

    You can use this to find all parents from node FLASH:

    tree
    Retrieving a Single Path

    With the nested set model, we can retrieve a single path without
    having multiple self-joins:

    SELECT parent.name
    FROM nested_category AS node,
            nested_category AS parent
    WHERE node.lft BETWEEN parent.lft AND parent.rgt
            AND node.name = 'FLASH'
    ORDER BY node.lft;
    
    +----------------------+
    | name                 |
    +----------------------+
    | ELECTRONICS          |
    | PORTABLE ELECTRONICS |
    | MP3 PLAYERS          |
    | FLASH                |
    +----------------------+
    

    This works because the child’s left will be in between its parents’ left and right.
    You can read further or search for modified preorder tree traversal.

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

Sidebar

Related Questions

I have db with column named link. Links have redirect urls, which I want
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a db table containing a column display_order . The data looks like
I have a SQL Server 2000 with a table containing an image column. How
I have a table containing around 100 columns, is it to possible to do
I have a file containing a lot of SQL statements, such as: CREATE TABLE
So I have a table that's basically a tree. Each row has a parent
I have a column containing the list of names. I need to select only
I have a table in a database with one column containing dates and another
I have a comma separated file named foo.csv containing the following data: scale, serial,

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.