Passing parameters to Mysql function

137 views Asked by At

I have created this function in mysql and the function is not working. The motive is to get highest score from a view containing student scores in each subject, in a class and in a term and a school session. The subjectid, termid, sessionid and class will be passed to the function from php. My goal is to have highest score returned.

CREATE FUNCTION highestscore (class CHAR(20),subjectid INT, termid AS INT,sessionid INT) 
      RETURNS INT 
   BEGIN
      SELECT Max(total) AS hscore 
      FROM reportsheetsubjectsview 
      WHERE class=class AND subjectid=subjectid 
      AND termid=termid AND sessionid=sessionid
      RETURN hscore;
   END

Edited stored procedure version

CREATE DEFINER=root@localhost PROCEDURE highestscore(IN subjectid INT, IN class VARCHAR(10), IN termid INT, IN sessionid INT, OUT highesttotalscore INT) DETERMINISTIC begin SELECT Max(total) INTO highesttotalscore FROM scoreview WHERE class=class
AND subjectid=subjectid AND termid=termid AND sessionid=sessionid; end

1

There are 1 answers

2
Jakob On

Add a GROUP BY clause to your select.

SELECT Max(total) AS hscore 
      FROM reportsheetsubjectsview 
      WHERE class=class AND subjectid=subjectid 
      AND termid=termid AND sessionid=sessionid GROUP BY class, subjectid, termid, sessionid