简体   繁体   中英

creating mysql function using phpmyadmin

I want to enter the following function to count a number of finished tasks from a mysql table in phpmyadmin, but its always returning a none descriptive error:

 DELIMITER $$
 CREATE FUNCTION `num_completed`(v1 INT)
 RETURNS INT
 BEGIN
 DECLARE icm INT;
 SELECT SUM(IF(completed='yes',1,0)) AS completed INTO icm FROM ri_t_course_progress WHERE enrollment_id=v1;
 RETURN icm;
 END$$

The query itself should be correct. I've tested it and returns the desired result. Anybody know whats wrong?

You need to assign the result of the SELECT into the variable. Here's one way:

 SELECT SUM(IF(completed='yes',1,0)) INTO icm 
 FROM ri_t_view_course_progress WHERE enrollment_id=v1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM