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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:49:06+00:00 2026-06-03T02:49:06+00:00

I have a mysql table named category. the basic structure looks like this- ——

  • 0

I have a mysql table named “category”. the basic structure looks like this-

------     --------     ------
cat_id     cat_name     parent
------     --------     ------
1          test1        NULL
2          test2        NULL
3          test3        2
4          test4        1
5          test5        3

now i want all the category data with parent category name (not only id) in a single query. is that possible? i could do it by using a second query (getting child’s parent name) in a while loop and merging data as a whole. but is it possible to do this with a single 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-06-03T02:49:07+00:00Added an answer on June 3, 2026 at 2:49 am

    Join the table with itself, using the parent column to link to the cat_id of the parent.

    SELECT c1.cat_id as childID, c1.cat_name ChildName, c2.cat_name as ParentName
    from category c1
    LEFT OUTER JOIN category c2
    ON c1.parent = c2.cat_id
    
    • Be careful: since some elements have no parents (NULL), I put a LEFT
      OUTER JOIN
      so those rows are displayed as well. If you don’t want
      that, use a JOIN instead of LEFT OUTER JOIN.
    • You can also show the lines, but display something else (empty or a
      text or …) instead of the NULL by using COALESCE.
    • You can consider the result as one (big) new table, so you can add WHERE clauses as you usually do, for example filtering on the parent name: WHERE c2.cat_name = 'test2'
    • 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 structured somewhat like this: type name value ===================== 1
I have a mysql database with a table structure like below : Table Name
I have this table named events in my mysql database: +-----+-----------+------------------------------+------------+ | ID |
I have an JPA entity like this: @Entity @Table(name = category) public class Category
I have multiple columns in a mySQL table. Three of the columns are named
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have this kind of mysql db (table name/fields) tbl_users : id, title, approved
i have MySql Table name 'category', in that table i have id catname parent_id
I have a mysql table id,name,gender,age religion( enum('HIN','CHR','MUS') ,category(enum('IND','AMR','SPA') where last 2 are enum
I have the following MySql Table: -------------------------------------------- Table 'category' -------------------------------------------- category_id name parent_id 1

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.