Reputation: 3409
It's again PHPMyAdmin... I try to run query on two tables - cards and ownerships (which list all players with the cards they own). And I'd like to create a function which will return (if given the name) the total amount of that card on the market. I defined this in the following way:
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//
Upvotes: 1
Views: 50
Reputation: 562260
The SET
syntax wants the right hand side to be a scalar, not a query, but you can convince it to run a scalar subquery:
SET res = (SELECT SUM(`ownerships`.`amount`)
FROM `ownerships` JOIN `cards` ON `ownerships`.`card_ID` = `cards`.`ID`
WHERE `cards`.`name` = _name
GROUP BY `name`);
Upvotes: 2