为什么PHPMyAdmin会在我的SQL语法中抛出错误?

时间:2014-03-29 17:18:44

标签: mysql

它再次是PHPMyAdmin ...我尝试在两个表上运行查询 - 卡和所有权(使用他们拥有的卡列出所有玩家)。我想创建一个函数,它将返回(如果给出名称)市场上该卡的总量。我用以下方式定义了这个:

CREATE FUNCTION  get_card_overall_count (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA

BEGIN
DECLARE res INT;
SET res = SELECT SUM(`ownerships`.`amount`) 
          FROM `ownerships` JOIN `cards` ON `ownerships`.`card_ID` = `cards`.`ID`
          WHERE `cards`.`name` = _name 
          GROUP BY `name`;
RETURN res;
END//

1 个答案:

答案 0 :(得分:2)

SET语法要求右侧是标量,而不是查询,但您可以说服它运行标量子查询:

SET res = (SELECT SUM(`ownerships`.`amount`) 
          FROM `ownerships` JOIN `cards` ON `ownerships`.`card_ID` = `cards`.`ID`
          WHERE `cards`.`name` = _name 
          GROUP BY `name`);