我正在尝试使用自定义函数将两个数字相乘并将结果插入表列中,但出现错误:
结果由多行组成
我遇到麻烦的代码:
INSERT INTO receipt(drug, amount, sum)
SELECT clientele.drug, clientele.amount, CalculateSum(clientele.amount, smallestprice.price)
FROM clientele, smallestprice
有什么想法吗?
编辑:
CalculateSum函数源代码:
SET GLOBAL log_bin_trust_function_creators = 1;
USE pharmacydb;
DELIMITER //
CREATE FUNCTION CalculateSum(amount INT, price DECIMAL)
RETURNS DECIMAL
BEGIN
DECLARE sum DECIMAL;
DECLARE local_otc BOOLEAN;
DECLARE local_amount INT;
SELECT otc FROM drug INTO local_otc;
SELECT amount FROM clientele INTO local_amount;
SET sum = 0;
IF local_otc = true THEN
SET sum = amount * price;
ELSEIF local_amount < amount THEN
SET sum = local_amount * price;
ELSE
SET sum = amount * price;
END IF;
RETURN sum;
END //
DELIMITER ;
答案 0 :(得分:2)
函数代码中的两个SELECT INTO中的一个(或两个)很可能返回一个以上的结果。 SELECT INTO表达式只能返回一行。您要从哪个drug
和clientele
分别获取otc
和amount
的值?