mysql - 使用 mysql 用户定义函数来计算美元金额。错误 1064 (42000)

标签 mysql user-defined-functions

我正在尝试创建一个 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/

相关文章:

mysql - 当前行为空时如何选择相关行?

mysql - 向服务器添加额外的 MySQL 数据文件夹。 Ubuntu

python - pyspark : KeyError when converting a DataFrame column of String type to Double

MySQL:LIMIT 是否减少了对用户定义函数的调用次数?

r - R中 SumIfs() 的向量

java - DB2 AS400 Java 函数始终返回相同的值

mysql - while 循环不适合我的教程示例

php - 一旦用户在 php 中使用, token 就必须从表中删除

php - Yii:按日期计算聚合

sql-server - SQL Server 2019 UDF 中的@@ROWCOUNT 错误