MySQL Multilevel parent child SP and IN Clause

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

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

for the above table data my desired result is

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

Answers


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....


Need Your Help

CSharpCodeProvider: Why is a result of compilation out of context when debugging

c# codedom

I have following code snippet that i use to compile class at the run time.