MYSQL函数参数对内部选择无效

wtzytmuj  于 2022-12-03  发布在  Mysql
关注(0)|答案(1)|浏览(103)

我正在创建一个函数,它在另一个SELECT中有一个SELECT。当我尝试使用函数参数之一作为内部SELECT的一部分时,它什么也没有返回。

FUNCTION `nota_alumno`(`uid` VARCHAR(255), `total_p` INT) RETURNS float
BEGIN
RETURN (SELECT (SUM(n_pregunta)*10)/total_p  AS nota
       FROM
       (SELECT nota_pregunta(scores.userid,question) as n_pregunta  FROM `scores` WHERE scores.userid=uid GROUP BY question) as T);
END

当我只输入一个非基于参数的值时,它确实可以工作:

FUNCTION `nota_alumno`(`uid` VARCHAR(255), `total_p` INT) RETURNS float
BEGIN
RETURN (SELECT (SUM(n_pregunta)*10)/total_p  AS nota
       FROM
       (SELECT nota_pregunta(scores.userid,question) as n_pregunta  FROM `scores` WHERE scores.userid="ddj239dkdsd" GROUP BY question) as T);
END

下面是函数nota_pregunta的定义,以供参考:

FUNCTION `nota_pregunta`(`userid` VARCHAR(255), `q` INT) RETURNS float
BEGIN
RETURN (SELECT ROUND(1/COUNT(*),2) FROM `scores` WHERE userid=userid and question=q);
END
f4t66c6m

f4t66c6m1#

子查询过多。开始_END也过多。

FUNCTION nota_alumno(uid VARCHAR(255), total_p INT) 
RETURNS float
RETURN ( SELECT SUM(nota_pregunta(userid, question)) * 10 / total_p  AS nota
         FROM scores 
         WHERE scores.userid=uid 
         GROUP BY question
         );

此外,函数nota_pregunta不正确(在WHERE userid=userid中比较参数和参数,即检查所提供的userid是否不为NULL),必须

FUNCTION nota_pregunta(userid VARCHAR(255), q INT) 
RETURNS float
RETURN ( SELECT ROUND(1/COUNT(*),2) 
         FROM scores 
         WHERE scores.userid=userid AND question=q);

相关问题