Since mysql functions cannot return tables/result sets therefore i created a stored procedure (Get_StudentsWithAllIndicators) that does the stuff i needed. Now, i need to use this stored procedure result inside the actual stored procedure(Find_MapDetails) like this.
select * from students where studentid in (Get_StudentsWithAllIndicators('7,8', 2));
but it does not work! If you are recommending creating a temporary table and insert values into it please tell me the syntax..thanks
Get_StudentsWithAllIndicators
CREATE PROCEDURE `Get_StudentsWithAllIndicators`(IN p_list VARCHAR(255), IN p_length int)
BEGIN
/*make query with the length of indicators in the list*/
DECLARE x INT;
SET x=1;
SET @queryMain = 'SELECT distinct studentid FROM studentindicators WHERE studentid IN ';
SET @queryWhere = '(SELECT studentid FROM studentindicators WHERE indicatorid = substring_index(\'';
SET @query = '';
WHILE x <= p_length DO
SET @query = CONCAT(@query, @queryWhere, p_list, '\', ",", 1)) AND studentId IN ');
SET x=x+1;
SET @lengthWithCommas = Length(p_list);
SET p_list = substr(p_list, instr(p_list, ',') + 1, @lengthWithCommas - instr(p_list, ','));
END WHILE;
/*remove last AND - note: no occurence of A after AND is expected*/
SET @query = CONCAT(@queryMain, LEFT(@query, LENGTH(@query) - LOCATE('A', REVERSE(@query))));
PREPARE stmt FROM @query;
EXECUTE stmt;
END