如何在 mysql 中设置 SELECT 语句的输出值?

How to SET an output value of a SELECT statement in mysql?

因此,我正在创建一个 mysql EVENT,用于计算 transaction_tbl 每月的销售额。我找不到我犯的错误。

这是我的查询:

DELIMITER |

 CREATE EVENT compute_sales
 ON SCHEDULE
      EVERY 1 MONTH STARTS '2018-10-01 22:00.00'
 DO
      BEGIN

 DECLARE mmsales DECIMAL(11, 2)
 SET mmsales = SELECT SUM(total_amount)
           FROM transaction_tbl
           WHERE MONTH(transaction_date) = MONTH(CURRENT_DATE()) AND
                YEAR(transaction_date) = YEAR(CURRENT_DATE())

 INSERT INTO sales_tbl (sales_year, sales_month, total_sales, time_frame) VALUES 
          (YEAR(CURRENT_DATE), MONTHNAME(CURRENT_DATE), mmsales, CURRENT_DATE)

 END |

 DELIMITER ;

这是我遇到的错误:

#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 'SET mmsales = SELECT SUM(total_amount) FROM transaction_tbl' at line 8

你需要括号:

SET mmsales = (SELECT SUM(total_amount)
               FROM transaction_tbl
               WHERE MONTH(transaction_date) = MONTH(CURRENT_DATE())
                 AND YEAR(transaction_date) = YEAR(CURRENT_DATE()));

此外,您应该以分号结束每个语句。