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

The Archive Base Latest Questions

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

For example I have the following MySQL table: parent_id | child_id ——————— 1 |

  • 0

For example I have the following MySQL table:

parent_id | child_id
---------------------
       1  |  4
       1  |  3
       1  |  5
       2  |  8
       3  |  7

I want to print out the parent and all its children in a format like below:

parent     |    child
---------------------
           |      4
        1  |      3
           |      5
---------------------
        2  |      8
---------------------
        3  |      7

Basically I just want to display the parent ONCE(Distinct) and list out all its children with PHP. Is it possible to retrieve the above result with just ONE SQL query? I can get the above result if I first query the parents and then recursively query the children using the parent ids but that would be alot more SQL queries hitting the DB.

Or, do I retrieve the result containing every parent_id & children_id and achieve the above result in PHP by using arrays. If so, please tell me how.

  • 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-03T11:06:50+00:00Added an answer on June 3, 2026 at 11:06 am

    Yes. Select normally and use the parents as keys in an array.

    //Query normally
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $result[$row["parent_id"]][] = $row["child_id"];
    }
    

    Or something similar.

    EDIT

    The display part would look something like this:

    <?php
    
    $result = array(
        1 => array(4, 3, 5),
        2 => array(8),
        3 => array(7)
    ); //Assuming you get a resultset like this.
    $rowIsOpened = false; //Indicates whether a row is currently opened.
    
    //I'm using $rowIsOpened because the row immediately after the rowspanned cell shouldn't be closed.
    
    echo <<<HTML
    <table>
        <thead>
            <tr>
                <th>Parent</th>
                <th>Children</th>
            </tr>
        </thead>
        <tbody>
    HTML;
    //Echo a bunch of HTML before actually looping
    
    foreach ($result as $parent => $children) {
        echo "<tr>";
        echo "<td rowspan=";
        echo count($children); //Span over <how many children are> rows
        echo ">$parent</td>";
        $rowIsOpened = true; //Row is opened
        foreach ($children as $child) {
            if (!$rowIsOpened) {
                echo "<tr>";
            } //Only open a row if row is not opened
            echo "<td>$child</td>";
            echo "</tr>";
            $rowIsOpened = false; //Row is now closed. Ready for next iteration.
        }
    
    }
    //Close the table tags etc.
    echo <<<HTML
        </tbody>
    </table>
    HTML;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following MySql Table: -------------------------------------------- Table 'category' -------------------------------------------- category_id name parent_id 1
I have a question about MySQL InnoDB. For example: I have the following table
I'm trying to figure out how XSLT process namespace prefixes and have following example:
I have the following example in a SQL table Cust Group Sales A 1
I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),
I have the following MySQL table: | id | Name | Windows | Linux
I have the following mysql table called test. lets say it has the data
I have the following mysql table -------------------------------------------------------- |id | hometeam |goalsfor|goalsagainst| awayteam | --------------------------------------------------------
Let's say I have the following MySQL table: id | species ------------ 1 |
I have the following table in MySQL... CREATE TABLE `visits` ( `id` int(10) unsigned

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.