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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:23:32+00:00 2026-05-28T06:23:32+00:00

Consider the following: Table – id, parentid What I’d like to do, is I’d

  • 0

Consider the following:

Table - id, parentid

What I’d like to do, is I’d like to pull all the children (not only direct children, but all of them, i.e. children of children of children etc.) of a specific parent.

So let’s say the table contains the following row: (2, 1), (3, 1), (4, 2), (5, 4)

Then for parentid = 1, the table would return ids 2, 3, 4 AND 5.

Is this possible?

If not (and I guess it’s indeed not possible), what are my options?

I really don’t want to use dozens of queries…

P.S. I can’t change the database structure.

Also, as there might be hundreds of thousands of records in the table, I can pull them all and do the whole thing using PHP instead.

  • 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-28T06:23:32+00:00Added an answer on May 28, 2026 at 6:23 am

    This might help:

    $parentId = 1; // the parent id
    $arrAllChild = Array(); // array that will store all children
    while (true) {
        $arrChild = Array(); // array for storing children in this iteration
        $q = 'SELECT `id` FROM `table` WHERE `parentid` IN (' . $parentId . ')';
        $rs = mysql_query ($q);
        while ($r = mysql_fetch_assoc($rs)) {
            $arrChild[] = $r['id'];
            $arrAllChild[] = $r['id'];
        }
        if (empty($arrChild)) { // break if no more children found
            break;
        }
        $parentId = implode(',', $arrChild); // generate comma-separated string of all children and execute the query again
    }
    print_r($arrAllChild);
    

    You may as well use recursion to do so but I think the above will need fewer iterations.

    Hope it helps!

    EDIT – I forgot to mention that you can as well implement the same logic in a MySQL stored procedure except that you cant use Arrays. The above example is implemented in PHP as you might have already guessed

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

Sidebar

Related Questions

Consider the following table: CREATE TABLE `prize` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `prize_details_id`
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Consider the following table and its indexes: CREATE TABLE 'Interaction' ( 'oid' bigint(20) NOT
Consider the following design: Company TABLE ( CompanyId int NOT NULL IDENTITY PRIMARY KEY,
Consider the following table: CREATE TABLE Participations ( roster_id INTEGER NOT NULL, round_id INTEGER
Consider the following table with a constraint that's a bit daft but simple enough
Consider the following SQL: CREATE TABLE USER1 ( pkUSER1_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
Consider the following table structure (not designed by me, and unchangable): +---------------+-----------------------------+---------------------------+ | the_id
Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type
Consider the following table: create table temp ( name int, a int, b int

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.