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

  • Home
  • SEARCH
  • 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 6811771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:22:03+00:00 2026-05-26T20:22:03+00:00

I have a MYSQL table called ‘categories’ from a project I inherited from someone

  • 0

I have a MYSQL table called ‘categories’ from a project I inherited from someone else.

id         parent_id       name
1          NULL            Travel
2          NULL            Sleep
3          NULL            Eat
4          NULL            Bath
5          1               Prams
6          1               Travel Systems
7          2               Cots
8          3               High Chairs

The table is obviously a lot bigger than that, but you get the general idea. I have a MYSQL statement which brings together this table with other category, brand and product tables, but basically I want to list the parent category name from the above table with the sub-category in the statement. How do I do this?

My current statement is something like:

    SELECT brands.name, products.name,  categories.id, categories.name, brands.id, 
FROM `products` , `brands` , `categories`
WHERE products.brand_id = brands.id
AND products.category_id = categories.id
AND brands.name = '$brand'
ORDER BY categories.name, products.name

How do I retrieve the parent category names in the results?
For example if the product is a Pram, how can I output “Travel”. I could do seperate MYSQL statements in the loop but I want to avoid this. This is either a stupidly simple question (in which case I apologise for being brain dead) or a little more complicated! Thanks.

  • 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-26T20:22:04+00:00Added an answer on May 26, 2026 at 8:22 pm

    First you need to know the parent id of the current category and then get the name for that id, you could use a subquery in this way:

    SELECT name FROM categories WHERE id = (SELECT pid FROM categories WHERE name = $brand)
    

    EDIT: Since you need to get the category and subcategory names in the same row for a given subcategory id, try this:

    SELECT sc.name AS subcategory, c.name AS category
    FROM categories sc
    LEFT JOIN categories c ON c.id = sc.parent
    WHERE sc.id = $subcategory_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mySQL table called test: create table test( locationExpect varchar(120) NOT NULL;
I have 1 mysql table called colors with rows id and name 1 -
I have a mysql database table called character with columns like name, strength, intelligence,
I have a mySQL table called clients with this structure: Name | Phone |
I have a MySql table called reviews which stores a lot of product reviews,
I have this mysql table called comments which looks like this: commentID parentID type
I have a legacy MySQL table called lnk_lists_addresses with columns list_id and address_id .
Hi I have a field in mySql table called jobnumber which auto increments with
I have a date and time column in my mysql table called start_date and
I have a MySQL db and inside it a table called ProductMaster with 6

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.