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

  • Home
  • SEARCH
  • 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 8067439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:17:38+00:00 2026-06-05T12:17:38+00:00

I am working on a table referrals which contains parent child relations I need

  • 0

I am working on a table referrals which contains parent child relations

enter image description here

I need to get the parent -> children -> ther children -> ….

for the above table data my desired result is

enter image description here

I have seen some code from SOF but didn’t get how they are working and tried myself with a very easy logic in my mind but unfirtunately its not working for a strange reason

I have written Stored Procedure for it but am stuck with an issue with the IN CLAUSE

DELIMITER $$

DROP PROCEDURE IF EXISTS `GetHierarchy3`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetHierarchy3`()
BEGIN
    DECLARE idss VARCHAR(225);
    SET @currentParentID := 999999;
    SET @lastRowCount := 0;

  ## A ##   
        INSERT INTO referrals_copy SELECT * FROM referrals WHERE uid1 = @currentParentID;

    SET @lastRowCount := ROW_COUNT();

 ## B ##
        SELECT GROUP_CONCAT(uid2)  INTO @idss FROM referrals WHERE  uid1 = @currentParentID;


    #SELECT @lastRowCount;
    SELECT * FROM referrals_copy;

    WHILE @lastRowCount > 0 DO
    SELECT "here";

        SELECT @idss;
  ## C ##
            INSERT INTO referrals_copy SELECT uid1, uid2 FROM referrals 
                WHERE uid1 IN (@idss);
            SET @lastRowCount := ROW_COUNT();
        #set @ids := NULL;
        SELECT @idss;
        SELECT GROUP_CONCAT(uid2) FROM referrals WHERE  uid1 IN (@idss);
        SELECT @idss;
        SET @lastRowCount := 0;

    END WHILE;


    -- return the final set now
    SELECT 
            *
        FROM referrals_copy;

END$$

CALL GetHierarchy3();

first I get the children for the main parent I want in the first query A, working fine

then, gets the children of main parent into variable @idss query B, working fine

then in the loop I use the variable idss in the query to get its children query C and put them in the resultant table… This is the problematic part

here for the very first iteration I have 1111,2222 in the variable idss which should result in insertion of values {3333, 4444} child of 1111, and {5555} child of 2222 in the table but it only put the children of 1111 ie 3333,4444 but, not 5555

I tried to replace variable from query C to check and it worked fine with values 1111,2222' withinIN Claues`

any idea on why the IN Clause is not accepting comma separated values from a group concat or any idea on fixing this code

Regards

  • 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-05T12:17:41+00:00Added an answer on June 5, 2026 at 12:17 pm

    Try this FIND_IN_SET(uid1,@idss)

    You have to replace this query part

    SELECT GROUP_CONCAT(uid2) FROM referrals WHERE uid1 IN (@idss);

    into this

    SELECT GROUP_CONCAT(uid2) FROM referrals WHERE FIND_IN_SET(uid1,@idss);

    i hope its work for you….

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

Sidebar

Related Questions

Once again I need some help. I'm working with a table that contains 3
I working on a table that keeps record of student attending course. The data
A site I'm working on takes table data, counts that table data and uses
While working with ActiveRecord I have a table which stores a serialized array of
I'm working on an table drill-down style iPhone app that has prepopulated data. I
I'm working with a table which has Name, FirstName, and Surname fields. Name was
I'm working with a table in MySQL that contains the following columns: id, january,
I'm working with table view and sections...In some case I need do remove one
I'm working no a site which stores individual page views in a 'views' table:
I am working with a table which is an extract of a set of

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.