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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:02:14+00:00 2026-06-04T12:02:14+00:00

Below is what I have in my table. myTable ++++++++++++++++++++ Parent + Child ++++++++++++++++++++

  • 0

Below is what I have in my table.

myTable

++++++++++++++++++++
Parent   +  Child
++++++++++++++++++++
  C1     +    G1
  C1     +    G2
  C1     +    G3
  G3     +    D1
  G3     +    D2
  C1     +    G4
  G4     +    D3
  G4     +    D4
  C2     +    G5
  C2     +    G6
  C2     +    G7
  C2     +    G8
 ++++++++++++++++++++

What, I want is as below using MYSQL.

C1
  G1
  G2
  G3
    D1
    D2
  G4
    D3
    D4

C2
  G5
  G6
  G7
  G8

Please let me know if this is possible in MYSQL. The output is something like TREE.

Update 1

If I get new table like below is also fine so that I can use this example.

++++++++++++++++++++++++++++++++++++++++
Parent   +  Child   + PLevel  + CLevel
++++++++++++++++++++++++++++++++++++++++
  C1     +    G1    +   1    +   2
  C1     +    G2    +   1    +   2
  C1     +    G3    +   1    +   2
  G3     +    D1    +   2    +   3
  G3     +    D2    +   2    +   3
  C1     +    G4    +   1    +   2
  G4     +    D3    +   2    +   3
  G4     +    D4    +   2    +   3
  C2     +    G5    +   1    +   2
  C2     +    G6    +   1    +   2
  C2     +    G7    +   1    +   2
  C2     +    G8    +   1    +   2
++++++++++++++++++++++++++++++++++++++++

NOTE : I have started level with 1 (in example I have level starting from 0). If I get this new table with level starting from 0 is also fine.

  • 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-04T12:02:16+00:00Added an answer on June 4, 2026 at 12:02 pm

    Although you can’t do with a single query, you can do with a stored procedure… The only pre-requirement, you need to add 2 more records to your existing sample table to represent that “C1” and “C2” ARE the top level… Add a record where the “Parent” field is blank, and the child level is “C1” and another for “C2”. This will “prepare” the top-most parent level. for subsequent hierarchy association, otherwise you have no starting “basis” of the top-level hierarchy. It also requires a “primary key” column (which I’ve created in this script as “IDMyTable” which is just 1-x sequential, but would assume you have an auto-increment column on your table to use instead).

    I’ve included all the output columns to show HOW it’s built, but the premise of this routine is to create a table based on the expected column outputs, yet extra to hold the hierarchical representation downstream as it’s being built. To MAKE SURE they retain the correct orientation as the layers get deeper, I’m concatinating the “ID” column — you’ll see how it works in the final result set.

    Then, in the final result set, I am pre-padding spaces based on however deep the hierarchy data is.

    The loop will add any records based on their parent being found in the preceding result set, but only if the ID has not already been added (prevent duplicates)…

    To see how the cyclical order was constantly appended to, you can run the last query WITHOUT the order by and see how each iteration qualified and added the previous hierarchy level was applied…

    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `GetHierarchy2`()
    BEGIN
        -- prepare a hierarchy level variable 
        set @hierlvl := 00000;
    
        -- prepare a variable for total rows so we know when no more rows found
        set @lastRowCount := 0;
    
        -- pre-drop temp table
        drop table if exists MyHierarchy;
    
        -- now, create it as the first level you want... 
        -- ie: a specific top level of all "no parent" entries
        -- or parameterize the function and ask for a specific "ID".
        -- add extra column as flag for next set of ID's to load into this.
        create table MyHierarchy as
        select 
                t1.IDMyTable,
                t1.Child AS Parent,
                @hierlvl as IDHierLevel,
                cast( t1.IDMyTable as char(100)) FullHierarchy
            from
                MyTable t1
            where
                    t1.Parent is null
                OR t1.Parent = '';
    
    
        -- how many rows are we starting with at this tier level
        set @lastRowCount := ROW_COUNT();
    
        -- we need to have a "primary key", otherwise our UPDATE
        -- statement will nag about an unsafe update command
        alter table MyHierarchy add primary key (IDMyTable);
    
    
        -- NOW, keep cycling through until we get no more records
        while @lastRowCount > 0 do
    
            -- NOW, load in all entries found from full-set NOT already processed
            insert into MyHierarchy
                select 
                        t1.IDMyTable,
                        t1.Child as Parent,
                        h1.IDHierLevel +1 as IDHierLevel,
                        concat_ws( ',', h1.FullHierarchy, t1.IDMyTable ) as FullHierarchy
                    from
                        MyTable t1
                            join MyHierarchy h1
                                on t1.Parent = h1.Parent
                        left join
                            MyHierarchy h2
                                on t1.IDMyTable = h2.IDMyTable
                    where
                        h2.IDMyTable is null;
    
    
            set @lastRowCount := row_count();
    
            -- now, update the hierarchy level
            set @hierLevel := @hierLevel +1;
    
        end while;
    
    
        -- return the final set now
        select 
                *, concat( lpad( ' ', 1 + (IDHierLevel * 3 ), ' ' ), Parent ) as ShowHierarchy
            from MyHierarchy
            order by FullHierarchy;
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in that there are relation between Parent and Child, in this
I have a table like this below: <tr> <th scope=col> Nome Completo </th> <th
I have a table in mysql like below : Table name : Emp Emp
Suppose I have a table MYtable as given below: ID A B C 1
I'm storing JSON data in a MySQL table using the code below. It works
I have a table as below; <table id=mytable> <tr> <td>cola1</td> <td>cola2</td> <td>cola3</td> <td class=rem>cola4</td>
I have a table as below; <table id=mytable cellpadding=0 cellspacing=0> <tr> <td>&nbsp;</td> <td><input type=text
I have a table like below <table id=mytable> <tr><th>checked</th><th>id</th><th>text</th></tr> <tr><td><input id=cb1 type=checkbox name=checker1/></td><td>123</td><td>abc</td></tr> <tr><td><input
I have a table like below: MyTable -------- A B C A,B,C are columns
Suppose I have a table view, and I want to implement something like this:

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.