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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:49:06+00:00 2026-06-16T09:49:06+00:00

How do I create a query that display every parent with its children rather

  • 0

How do I create a query that display every parent with its children rather than to display all parents first then children. For example, right now, the mySQL table shows all COMPUTERS and then DRIVES. I want it to display for every computer, display the drives associated with that computer. I.E.:

 COMPUTER   DRIVE        PID         ID 
 dell                    1 
 apple                   2
 apple                   3
 hp                      4
 hp                      5
            WD300FV                  1
            WD440GB                  1
            WD55                     2
            WD44X                    2...

I want it displayed like this where every computer is shown a list of its drives:

 COMPUTER   DRIVE        PID     
 dell                    1 
            WD300FV      1
            WD440GB      1
 apple                   2
            WD55         2
            WD44X        2
 apple                   3.....

Where computer has a parent ID and drive has an ID.

When I do this query SELECT * FROM table it doesn’t create rows as I would like it to be displayed. I want every row that shows a computer to display a list of drives below the computer row.

  • 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-16T09:49:07+00:00Added an answer on June 16, 2026 at 9:49 am

    I would question your table structure. I would think computers and drives should be in two separate but related tables, since they are two different things, potentially with their own properties.

    computer table:

    computer_id |  manufacturer
    ---------------------------
    1           |  dell
    2           |  apple
    3           |  hp
    

    computer_id would be auto-incrementing primary key

    drive table:

    drive_id   |  computer_id  |  name
    -----------------------------------
    1          |  1            | WD300FV
    2          |  1            | WD440GB
    3          |  2            | WD55
    4          |  2            | WD44X
    5          |  3            | XYZ
    

    drive_id would be auto-incrementing primary key

    computer_id would have index (and possibly foreign key constraint if you are using InnoDB and wanted to enforce a relationship to computer table)

    Then you would query like this:

    SELECT c.computer_id, c.manufacturer, d.drive_id, d.name
    FROM computer AS c
    INNER JOIN drive AS d ON c.computer_id = d.computer_id
    ORDER BY c.computer_id ASC, d.drive_id ASC
    

    If, for whatever reason, you didn’t want to go with a normalized structure like I have proposed and instead use a de-normalized structure like similar to what you already have, you should at least consider doing your de-normalization in a more sane way. There is no need for your pid logic at all if you just structured your table like this:

    computer  |  drive
    ------------------
    dell      | WD300FV
    dell      | WD4440GB
    apple     | WD55
    apple     | WD44X
    hp        | XYZ
    

    You are just adding a level of complexity that doesn’t need to be there. You could easily sort your result by “computer”:

    SELECT computer, drive FROM table
    ORDER BY computer ASC, drive ASC
    

    With no schema changes at all you could query like this:

    SELECT computer, drive, CASE WHEN pid = '' THEN id ELSE pid END as `sort_id`
    FROM table
    ORDER BY `sort_id` ASC, drive ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a created_at query range that will capture all records created
I am trying to create a query that will display the name, dep no.
I want to create a query that selects the columns PlayerName , PlayerNumber, Location,
I would like to create a query that returns rows in the following format:
I've been trying now for some time to create a query that would count
Can anyonen help with Teradata? I want to create a query that is a
I want to create an SQL query that selects the pictures of females in
I am looking to create a SQL query that collects the results from multiple
I want to create a query in TFS that allows me to view only
I made a query that retrieve a list of items from a database: CREATE

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.