I have table name pctable for parent child category.

```
  id      parent_id
  2322    0
  2323    2322
  2324    2322
  2335    2322
  2336    2322
  2337    2322
  4869    2322
  5121    2322
  6033    2322
  6783    2322
  1061    2323
  4870    4869
  4871    4869
  4872    4869
  4873    4869
  6034    6033
  6059    6033

```

I wrote a mysql function to get all parent child in comma separated string.

```
DELIMITER $$
    DROP FUNCTION IF EXISTS getBaseID $$
    CREATE FUNCTION getBaseID(articleID varchar(1024)) RETURNS TEXT
    BEGIN
        DECLARE x TEXT;
        DECLARE y TEXT;
        DECLARE rtext TEXT;
        SET rtext = "";
        SET x = articleID;
        sloop:LOOP
            SET y = NULL;
            SELECT SQL_CACHE GROUP_CONCAT(id) INTO y FROM pctable WHERE parent_id IN(x);
            IF y IS NULL THEN
                LEAVE sloop;
            END IF;  
            SET x = y;
            SET rtext = CONCAT(rtext,',',x);
            ITERATE sloop;
        END LOOP;
        RETURN rtext;
    END $$

    DELIMITER ;

```

When I am calling function it is returning wrong data.

```
  SELECT getBaseID(2322) FROM pctable LIMIT 1;

```

It's returning 

```
 "2323,2324,2335,2336,2337,4869,5121,6033,6783,1061"

```

But it should return 

```
  "2323,2324,2335,2336,2337,4869,5121,6033,6783,1061,4870,4871,4872,4873,6034,6059"

```