mysql - 使用查询在 mysql 中创建事件?
问题描述
我想在 mysql 中创建一个事件,但想添加我在描述中放入的总和和累积查询
这是事件代码,但我不知道该怎么做:
CREATE EVENT `recurring data`
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
BEGIN
INSERT INTO ....
END
这是查询:
SET @csum := 0, @product_id:=NULL;
UPDATE sma_sale_items
SET acumulado = (@csum := if(product_id=@product_id,@csum, 00000.0000) + quantity), product_id=(@product_id:=product_id)
ORDER BY product_id, id, sale_id;
更新
我尝试创建此事件,但它不起作用,它告诉我一个错误
1 CREATE EVENT `recurring data`
2 ON SCHEDULE
3 EVERY 1 MINUTE
4 STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
5 DO
6 BEGIN
7 SET @csum := 0, @product_id:=NULL;
8 UPDATE sma_sale_items
9 SET acumulado = (@csum := if(product_id=@product_id,@csum, 00000.0000) + quantity), product_id=(@product_id:=product_id)
10 ORDER BY product_id, id, sale_id;
11 END
错误代码
#1064 - Something is wrong in its sintax near '' on line 7
解决方案
CREATE EVENT `recurring data`
ON SCHEDULE
-- how often it must be executed
EVERY 1 MINUTE
-- when the first execution must be performed
STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
DO
BEGIN
-- perform the actions
END
测试
CREATE EVENT `recurring data`
ON SCHEDULE
EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
DO
BEGIN
DECLARE var_csum INTEGER DEFALUT 0;
DECLARE var_product_id DECIMAL(10,4) DEFAULT NULL;
UPDATE sma_sale_items
SET acumulado = (var_csum := if(product_id=var_product_id,var_csum, 00000.0000) + quantity),
product_id = (var_product_id:=product_id)
ORDER BY product_id, id, sale_id;
END
推荐阅读
- visual-studio - 尝试添加对 Visual Studio 项目的引用突然引发异常
- ruby-on-rails - Rails 单个包含多个类
- kubernetes - 为什么 Dask 客户端说我的集群具有比实际可用总数更多的内核和内存?
- python - 为什么这段代码在 for 循环中只使用 x 而不是 x 和 y?
- swift - 在 Array.first 中隐式展开选项
- sympy - 在 SymPy 中收集理性的非符号系数
- java - 从 openjdk11 访问 com.sun.* 类
- excel - 用于复制并粘贴到 BS 列的下一个空行的宏
- pybind11 - VS2017 与 pybind11 一起使用时会引发链接器错误
- c# - 等待第三方 API 回调