gpt4 book ai didi

mysql - 使用声明的变量返回函数时出错

转载 作者:行者123 更新时间:2023-11-29 09:28:11 25 4
gpt4 key购买 nike

因此,我尝试创建一个函数来创建可变的增长百分比,然后使用该值来生成来年一个月的预测。这是我的代码:

CREATE FUNCTION getPredictedSales(forecastmonth int(2))
RETURNS DOUBLE(6,2)
BEGIN
SELECT @percentincrease = (SELECT (SUM(s19.totalsales) / SUM(s18.totalsales))
FROM Sales2018 s18
INNER JOIN Sales2019 s19 ON s18.month = s19.month AND s18.shopname = s19.shopname
WHERE s18.month = forecastmonth)

RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE month = 1));

END

这是错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE' at line 9

最佳答案

您必须在每个语句末尾添加分号。而且你的返回率很高。

所以看起来应该是这样。

DELIMITER $$
CREATE FUNCTION getPredictedSales(forecastmonth int(2))
RETURNS DOUBLE(6,2)
BEGIN
SELECT @percentincrease = (SELECT (SUM(s19.totalsales) / SUM(s18.totalsales))
FROM Sales2018 s18
INNER JOIN Sales2019 s19 ON s18.month = s19.month AND s18.shopname = s19.shopname
WHERE s18.month = forecastmonth);

RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE month = 1);
END $$
DELIMITER ;

关于mysql - 使用声明的变量返回函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59230280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com