插入后选择count(*)并选择sum(my_data)-错误语法#1064

时间:2019-05-20 17:30:33

标签: mysql triggers

我是sql的初学者,甚至是编程方面的新手,最近我都发现了触发器,并且我想计算活动行的最大数量以及活动权重的总和 ps:活动记录定义为'cancled = 0' 我在声明变量(第2行)的行附近出现了错误#1064

我正在将其运行到PHPMYADMIN(MySQL)触发器中。

BEGIN
DECLARE @max_rows int;
DECLARE @max_tare int;
SET @max_rows = SELECT COUNT(*) FROM history WHERE cancled = 0;
SET @max_tare= SELECT SUM(tare) FROM history WHERE cancled = 0;
UPDATE max_tare
SET max_row  = @max_rows,
    max_tare = @max_tare
WHERE id = 1
END

我期望我用最大行数和活动权重的总和来更新第1行,但是我有一个错误

  

MySQL回复:#1064-'@max_rows int附近的语法错误;宣布   @max_tare int; SET @max_rows =从他的第2行中选择COUNT(*)

在Paul评论后,我将代码更改为:

BEGIN
DECLARE max_r int;
DECLARE max_t int;
SET max_r = SELECT COUNT(*) FROM history WHERE cancled = 0;
SET max_t = SELECT SUM(tare) FROM history WHERE cancled = 0;
UPDATE max_tare
SET max_row  = max_r,
    max_tare = max_t
WHERE id = 1
END

这是新错误:

  

MySQL回复:#1064-SELECT COUNT(*)FROM附近的语法错误   取消的历史记录= 0; SET max_t =第4行的SELECT SUM(皮重)FR'

提前

ty。

1 个答案:

答案 0 :(得分:0)

感谢@paul Spiegel和@forpas的帮助 这段代码帮助解决了问题

IF(NEW.mix != '') THEN
UPDATE max_tare
SET max_row  = (SELECT COUNT(*)  FROM history WHERE nom LIKE NEW.mix),
    max_tare = (SELECT SUM(tare) FROM history WHERE cancled = 0)
WHERE id = 1;
ELSE
UPDATE max_tare
SET max_row  = (SELECT COUNT(*)  FROM history WHERE cancled = 0),
    max_tare = (SELECT SUM(tare) FROM history WHERE cancled = 0)
WHERE id = 1;
END IF;
END

这对我来说很好,谢谢大家