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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:01:35+00:00 2026-05-30T10:01:35+00:00

I have a database table, where I have single table to manage all root

  • 0

I have a database table, where I have single table to manage all root level and their related sub categories.

Below is the table and data structure of the same, My SQL table:

id  label   pid
        1   Parent 1    0
        2   Parent 2    0
        2   Parent 2    0
        3   Child 1.1   1
        4   Child 1.2   2
        5   Child 2.1   2
        6   Child 1.1.1 3
        7   Child 1.1.2 3

Now, I want a single query, that gives me result, where first records are my root level category labels where pid is 0 and then below that its subcategory, whose pid is 1 and pid as label, instead of id to display in my dropdown on add page

Hope you guys understand what I am trying to say here !

Looking forward for your quick response.

Edit from comments

Tried so far, but without success, as it produces a single level of results.

SELECT p.Name, s.Name FROM Categories s LEFT JOIN Categories p ON s.mainCat = p.ID ORDER BY p.Name, s.Name;
  • 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-30T10:01:36+00:00Added an answer on May 30, 2026 at 10:01 am

    The short version was that you seem to be trying to do a hierarchical tree, and whilst I have tried various methods out on this the one desribed on the following link seems the most efficient in the long run even though it takes a bit more work to set up in the first place.

    http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

    For more details I’d advise reading the above article, and possibly this question which covers a similar problem.

    Create a multiselect drop down with parent-child menu items

    (Direct link to the answer I gave there, says pretty much the same but with a lot more explanation/rationale)

    https://stackoverflow.com/a/9377018/1213554

    At present your key problem is that there is no clear linking field that tells you which child links to which parent. Ideally this should be done either using the tree method detailed above, or at the most basic by having a parentID field which can be either 0 (root element) or equating to the PRIMARY KEY id column. This in turn allows you to more easily determine what each childs parent is.

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

Sidebar

Related Questions

Let's say you have a database with a single table like... --------------------------------------------- | Name
I have a database containing a single huge table. At the moment a query
I have a fairly huge database with a master table with a single column
I have a database table full of some really ugly and messy data. In
MYSQL Database: I have a table of data that I need to put into
I have a database with a single table right now, which contains the following
In my Android application; I have a single database with multiple tables. Each table
I have several database tables that just contain a single column and very few
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table and one of the fields (not the primary key)

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.