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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:32:58+00:00 2026-06-11T21:32:58+00:00

My terminology is somewhat lacking, so the title for my question is undoubtedly kind

  • 0

My terminology is somewhat lacking, so the title for my question is undoubtedly kind of lame, but I will explain what I mean below.

I have a MySQL table that looks something like the following:

categories:

category_id | parent_id

     0            0
     1            0
     2            1
     3            1
     4            3

Now, what I want to do is output the category structure like this:

category structure:

0
1 -> 2
     3 -> 4

In addition to needing to be able to display the category structure, if a category is selected then I want to find all of the articles in that category and in the subcategories (articles would be another table where each article would have a parent_category_id liking it to the category it’s in).

The only way I can think of doing this is:

  1. Get all categories with parent_id equal to the id of the category being viewed
  2. Loop through all of the results and repeat step one
  3. Just keep doing that until all of the results have been checked

Is there a better way to do this?

  • 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-06-11T21:32:59+00:00Added an answer on June 11, 2026 at 9:32 pm

    one way to do it in an efficient way is using nested sets.
    it is a little bit tricky, and its a bit more complicated to update.

    It works like that:

    every node has 2 id’s, and a level. all child nodes ids ar between the nodes ids.
    example:

    category_id | parent_id | low_id |  high_id 
     0            0          1          2
     1            0          3          10
     2            1          4          5
     3            1          6          9
     4            3          7          8
    

    now you can say “give me all child nodes of category 1”:

    select *
    from categories
    where low_id between 3 /* (low_id node1) */ and 10 /* (high_id node 1) */
    

    but if you have to insert an node, you need an algorithm to move the other nodes in right position.
    also it is good to store the level of the nodes, so you don’t have to look for the id/parent_id relationship, you only have to sort by low_id and use the level as indicator.

    there is a doctrine2 plugin to handle nested sets if you use php as programming language.

    edit: i think this is a good point to start: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

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

Sidebar

Related Questions

I may have the terminology wrong in my question, but here's my best attempt:
I might have my terminology incorrect in the title using the word singleton. I
I have terminology/modeling question about representing components in a service. Consider.. Scenario A: ICatalogService
I am confused about the terminology of the below trees, I have been studying
I'm struggling to find the right terminology here, but if you have jQuery object...
I'm working in Delphi (and Delphi terminology), but my question is language neutral. Suppose
I may not be using the proper terminology but hopefully I get my question
I'm somewhat new to automation. I don't know any programming languages officially, but have
Just a quick question about terminology: in the example below (pulled from Apple documentation),
Terminology question ! In many graphics packages the user can draw a line between

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.