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

b0zn9rqh  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(327)

因此,我正在创建一个mysql事件,它从事务\u 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-您的sql语法有错误;在第8行的“set mmsales=select sum(total\u amount)from transaction\u tbl”附近,检查与mysql服务器版本对应的手册,以获得正确的语法

pb3s4cty

pb3s4cty1#

你需要括号:

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

另外,每个语句都应该以分号结尾。

相关问题