Use curson and print output in store procedure
DECLARE _output TEXT DEFAULT '';
DECLARE _ID INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT ID FROM CodeID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO _ID;
IF NOT done THEN
SET _output = CONCAT(",", _ID); /*SEE ALT BELOW*/
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
SELECT _output;
Loop in store procedures
http://www.mysqltutorial.org/stored-procedures-loop.aspx