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

The Archive Base Latest Questions

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

I have a MySQL table of following structure. **Table elements :** element_id element_name parent_id

  • 0

I have a MySQL table of following structure.

**Table elements :**

element_id  element_name    parent_id
1           UIG             0
2           CAM             1
3           IHG             1
4           USR             1
5           DBL             1
6           APD             1
7           RTM             1
8           OCR             2
9           IRT             3
10          ICR             3
11          OCR             2
12          USH             1
13          AML             1

I need to find child elements of a given element.

I made the following query :

SELECT parent_id,GROUP_CONCAT(element_id)
FROM elements
WHERE parent_id='1'
GROUP BY parent_id

which returns,

+-----------+--------------------------+
| parent_id | GROUP_CONCAT(element_id) |
+-----------+--------------------------+
|         1 | 2,3,4,5,6,7,12,13        | 
+-----------+--------------------------+
1 row in set (0.00 sec)

While I need also need the childs of element 2 and 3, which should result into

+-----------+------------------------------------+
| parent_id | GROUP_CONCAT(element_id)           |
+-----------+------------------------------------+
|         1 | 2,3,4,5,6,7,8,9,10,11,12,13        | 
+-----------+------------------------------------+
1 row in set (0.00 sec)

How do I achieve this without procedures and just a 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-16T16:49:45+00:00Added an answer on June 16, 2026 at 4:49 pm

    You just need to use the in operator on a subquery.

    select group_concat(element_id)
    from chpr
    where parent_id in (
      select group_concat(element_id)
    from chpr
    where parent_id = 1
    group by parent_id)
    ;
    

    ** AS per OP’s comment the levels are definitely a concern**

    However based on the initial sample data and request, here is the SQLFIDDLE DEMO that provides the results as per the question’s expected output.

    The only change is that one needs to group by both element_id and parent_id the innner most first subquery.

    Not a very elegant query at all:

    select 1 as parent_id, group_concat(x.element_id)
    from (
    (select element_id
    from chpr
    where parent_id in
    (select element_id
    from chpr
    where parent_id = 1
    group by element_id, parent_id
    ))
    union all
    (select element_id
    from chpr
    where parent_id = 1
    group by element_id, parent_id
    )) x
    ;
    

    Results:

    PARENT_ID   GROUP_CONCAT(X.ELEMENT_ID)
    1           8,9,10,11,2,3,4,5,6,7,12,13
    
    • 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 structure: num field company phone website 1 Gas
I have a MySQL table with a structure like the following: I'm looking for
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have a following mysql table with a lot (3000+) of values: [id] [parent_id]
I have a MySql table having the following structure: ontology_term pathway_id pathway_name I want
I have a MySQL database with the following table structure: TransactionType : Transaction_Amount, Transaction_ID,
I have MySQL table with the following structure: +---------+--------------+------+-----+---------+----------------+ | Field | Type |
I have a MySQL table with the following structure: I want a query that
I Have a table 'C' in MySQL db with the following structure: Object1Id (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.