功能" SUM不存在"

时间:2016-09-03 18:00:18

标签: mysql .net

当我执行下面的脚本时,我正在使用mysql 5.5.11

INSERT INTO payments(created,  Amount,  user,  Remarks,  orderid,  paymethod) 
VALUES('2016-09-03', 0.0, 'admin', '', 4, 'Cash');

我收到错误

  

SQL错误:功能mydb.SUM不存在。检查'功能名称   解析和解决方案'参考手册中的部分

这是表格架构

CREATE TABLE payments (
  ID         int AUTO_INCREMENT NOT NULL,
  OrderID    int,
  Amount     decimal(11,2),
  Created    varchar(20),
  Remarks    varchar(160),
  user       varchar(60),
  PayMethod  varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci,
  /* Keys */
  PRIMARY KEY (ID)
) ENGINE = InnoDB;

可能是错误的原因

这是附在表格上的触发器

BEGIN
  /* Trigger text */
  UPDATE Orders Set Paid =(Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID),PayMethod =new.PayMethod  WHere Orders.id = new.OrderID;
   UPDATE Orders Set Bal = Total - Paid  WHere Orders.id = new.OrderID;
END

2 个答案:

答案 0 :(得分:21)

看看触发器:

UPDATE Orders Set Paid =(Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID),PayMethod =new.PayMethod  WHere Orders.id = new.OrderID;

MySQL不接受函数名和括号之间的空格(除非你设置了SQL_MODE = IGNORE_SPACE,但这会产生其他不良副作用)

您可以通过运行:

来检查SQL_MODE设置
SHOW GLOBAL VARIABLES LIKE 'SQL_MODE';

关于此的MySQL文档:link

一些例子:

mysql> select sum(5);
+--------+
| sum(5) |
+--------+
|      5 |
+--------+
1 row in set (0.00 sec)

mysql> select sum (5);
ERROR 1305 (42000): FUNCTION sum does not exist

解决方案:删除空格

UPDATE Orders Set Paid =(Select SUM(Amount) From Payments AS p Where p.OrderID  = Orders.ID),PayMethod =new.PayMethod  WHere Orders.id = new.OrderID;

答案 1 :(得分:3)

按以下步骤删除SUM(之间的空格:Select SUM(Amount) From Payments