我正在尝试创建一个 mysql 函数来计算商店中前五名顾客的总支出,但我不断收到以下语法错误。是什么导致了错误? 错误:
ERROR 1064 (42000): 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
'FUNCTION costofbestbuyers (totalspent DECIMAL)
RETURNS DOUBLE
BEGIN
DECLA' at line 1
功能:
DELIMITER //
CREATE FUNCTION topfivespenders (totalspent DECIMAL)
RETURNS DOUBLE
BEGIN
DECLARE totalspent DOUBLE DEFAULT 0;
SELECT sum(ordercost) AS totalspent
FROM customer c JOIN orders o
ON c.customerID = o.cID
GROUP BY o.cID
ORDER BY totalspent desc
LIMIT 5;
RETURN totalspent;
END; //
最佳答案
将 sum(ordercost)
值设置为 totalspent
变量的语法不正确。
试试这个。希望这有效。
DELIMITER //
CREATE FUNCTION topfivespenders (totalspent DECIMAL)
RETURNS DOUBLE
BEGIN
DECLARE totalspent DOUBLE DEFAULT 0;
SELECT sum(ordercost) INTO totalspent
FROM customer c JOIN orders o
ON c.customerID = o.cID
GROUP BY o.cID
ORDER BY totalspent desc
LIMIT 5;
RETURN totalspent;
END//
DELIMITER //
关于mysql - 使用 mysql 用户定义函数来计算美元金额。错误 1064 (42000),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757422/