I'm trying to create a MySQL stored function that returns a "jobMatch" score. I'm new to procedures, functions, triggers, and events. I have a few functions working, one calculates distance using the haversine formula and works fine.
I receive the error:
#2014 - Commands out of sync; you can't run this command now
when calling the function within a query like this:
SELECT uID,jobMatchScore(uID,'43.665600','-79.383000') AS matchScore FROM `JW-USER-PROFILES`;
Basically I would like to call the jobMatchScore function for each userID in the query as eventually I would like to order the result by jobMatchScore.
My jobMatchScore function looks like this:
BEGIN
DECLARE distance INT(4);
DECLARE matchScore INT(4);
DECLARE posRating INT(4);
DECLARE negRating INT(4);
DECLARE totalRatings INT(4);
DECLARE ratingPercent DECIMAL(3,2);
SET distance=0;SET matchScore=0;SET posRating=0;SET negRating=0;SET ratingPercent=0.00;SET totalRatings=0;
SELECT geoDist(uLat,uLon,input_jLat,input_jLon) INTO distance FROM `JW-USER-PROFILES` WHERE uID=input_uID LIMIT 1;
CASE WHEN distance BETWEEN 0 AND 10 THEN SET matchScore=matchScore+100;
WHEN distance BETWEEN 10 AND 20 THEN SET matchScore=matchScore+75;
WHEN distance BETWEEN 20 AND 30 THEN SET matchScore=matchScore+50;
WHEN distance BETWEEN 30 AND 50 THEN SET matchScore=matchScore+25;
WHEN distance >= 50 THEN SET matchScore=matchScore+10;
END CASE;
SELECT COUNT(rID) INTO posRating FROM `JW-USER-RATINGS` WHERE uRating >=3 AND uID=input_uID;
SELECT COUNT(rID) INTO negRating FROM `JW-USER-RATINGS` WHERE uRating < 3 AND uID=input_uID;
SET totalRatings=posRating+negRating;
SET ratingPercent=posRating / totalRatings * 100;
CASE WHEN ratingPercent BETWEEN 90 AND 100 THEN SET matchScore=matchScore+100;
WHEN ratingPercent BETWEEN 80 AND 90 THEN SET matchScore=matchScore+90;
WHEN ratingPercent BETWEEN 70 AND 80 THEN SET matchScore=matchScore+80;
WHEN ratingPercent BETWEEN 60 AND 70 THEN SET matchScore=matchScore+70;
WHEN ratingPercent BETWEEN 50 AND 60 THEN SET matchScore=matchScore+60;
WHEN ratingPercent BETWEEN 40 AND 50 THEN SET matchScore=matchScore+50;
WHEN ratingPercent BETWEEN 30 AND 40 THEN SET matchScore=matchScore+40;
WHEN ratingPercent BETWEEN 20 AND 30 THEN SET matchScore=matchScore+30;
WHEN ratingPercent BETWEEN 10 AND 20 THEN SET matchScore=matchScore+20;
WHEN ratingPercent BETWEEN 0 AND 10 THEN SET matchScore=matchScore+10;
END CASE;
RETURN matchScore;
END
There are some input parameters "input_uID" (integer),"input_jLat" (decimal),"input_jLon" (decimal).
I've done some reading about the error and it seems to be related to a not freed result.
When I hard code a uID into the function call it does return the expected jobMatchScore.
i.e.
SELECT uID,jobMatchScore(12,'43.665600','-79.383000') AS matchScore FROM `JW-USER-PROFILES`;
Anyone else run into this or know how to resolve the issue or perform this sort of functionality?