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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:47:08+00:00 2026-05-19T14:47:08+00:00

I have a categories table that looks like this: id | name | parent

  • 0

I have a categories table that looks like this:

id | name       | parent    
-----------------------    
1  | Toys       | 1
2  | Clothing   | 1
3  | Kid's Toys | 0 

I have another table called category_relationships which looks like this:

id | category_id | parent_id    
----------------------------    
1  | 3           | 1

I want to have the following output:

Categories:

Toys
  - Kid's Toys
Clothing

How to achieve this with one query?

  • 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-19T14:47:08+00:00Added an answer on May 19, 2026 at 2:47 pm

    A better/proper/robust answer will probably be create a MySQL PROCEDURE for this, but if your data can fit in these limitations, you can use the below:

    • no more than 5 levels (or expand the pattern as required)
    • IDs are no more than 6 digits (or change the concat expressions)

    This query uses Concat to build a sortable reference so that children of A come after A etc. The names are indented manually using concat and leading spaces.

        select concat(1000000 + a.id, '|') SORT
              ,a.name
        from categories a
        where a.parent = 1 # top level parents only
    union all
        select concat(1000000 + a.id, '|', 
                 1000000 + IFNULL(b.id,0), '|')
              ,concat('  - ', b.name)
        from categories a
        inner join category_relationships a1 on a1.parent_id = a.id
        inner join categories b on b.id = a1.category_id
        where a.parent = 1
    union all
        select concat(1000000 + a.id, '|', 
                 1000000 + IFNULL(b.id,0), '|',
                 1000000 + IFNULL(c.id,0), '|')
              ,concat('    - ', c.name)
        from categories a
        inner join category_relationships a1 on a1.parent_id = a.id
        inner join categories b on b.id = a1.category_id
        inner join category_relationships b1 on b1.parent_id = b.id
        inner join categories c on c.id = b1.category_id
        where a.parent = 1
    union all
        select concat(1000000 + a.id, '|', 
                 1000000 + IFNULL(b.id,0), '|',
                 1000000 + IFNULL(c.id,0), '|',
                 1000000 + IFNULL(d.id,0), '|')
              ,concat('      - ', d.name)
        from categories a
        inner join category_relationships a1 on a1.parent_id = a.id
        inner join categories b on b.id = a1.category_id
        inner join category_relationships b1 on b1.parent_id = b.id
        inner join categories c on c.id = b1.category_id
        inner join category_relationships c1 on c1.parent_id = c.id
        inner join categories d on d.id = c1.category_id
        where a.parent = 1
    union all
        select concat(1000000 + a.id, '|', 
                 1000000 + IFNULL(b.id,0), '|',
                 1000000 + IFNULL(c.id,0), '|',
                 1000000 + IFNULL(d.id,0), '|',
                 1000000 + IFNULL(e.id,0))
              ,concat('        - ', e.name)
        from categories a
        inner join category_relationships a1 on a1.parent_id = a.id
        inner join categories b on b.id = a1.category_id
        inner join category_relationships b1 on b1.parent_id = b.id
        inner join categories c on c.id = b1.category_id
        inner join category_relationships c1 on c1.parent_id = c.id
        inner join categories d on d.id = c1.category_id
        inner join category_relationships d1 on d1.parent_id = d.id
        inner join categories e on e.id = d1.category_id
        order by SORT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I have a table called categories that looks like this: id name
I have a categories table that looks like this: ---------------------------------------- | id | parentId
I have a table categories that has: id, name, subcategory_id, parent_id . Another table
I have a table that looks something like this: ID | Keyword | Category
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have 3 tables that look like this: tblVideo: VideoID | Video Name 1
I have 2 tables. The first one, called 'users' looks like this: id |
I have a table that looks like: What I want to do is output
I have a Categories table in which each category has a ParentId that can

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.