mysql - 在 MySQL 中创建计划事件

标签 mysql sql mysql-workbench mysql-error-1064 mysql-event

我在名为 fouras 的 MySQL 数据库中提供了一个表名:

mysql> desc offer;
+------------------------+---------------+------+-----+---------+----------------+
| Field                  | Type          | Null | Key | Default | Extra          |
+------------------------+---------------+------+-----+---------+----------------+
| id                     | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| description            | varchar(5000) | NO   |     | NULL    |                |
| end_date               | date          | NO   |     | NULL    |                |
| is_accepted            | bit(1)        | NO   |     | NULL    |                |
| is_active              | bit(1)        | NO   |     | NULL    |                |
| is_draft               | bit(1)        | NO   |     | NULL    |                |
| is_processed           | bit(1)        | NO   |     | NULL    |                |
| is_removed             | bit(1)        | NO   |     | NULL    |                |
| max_reservation_number | int(11)       | NO   |     | NULL    |                |
| promotion_first_param  | varchar(255)  | YES  |     | NULL    |                |
| promotion_product      | varchar(255)  | YES  |     | NULL    |                |
| promotion_second_param | varchar(255)  | YES  |     | NULL    |                |
| promotion_type         | varchar(255)  | NO   |     | NULL    |                |
| publish_date           | date          | YES  |     | NULL    |                |
| remove_time_stamp      | bigint(20)    | YES  |     | NULL    |                |
| start_date             | date          | NO   |     | NULL    |                |
| title                  | varchar(255)  | NO   |     | NULL    |                |
| views_number           | int(11)       | YES  |     | NULL    |                |
| local_business         | bigint(20)    | YES  | MUL | NULL    |                |
+------------------------+---------------+------+-----+---------+----------------+
19 rows in set (0.00 sec)

现在,我想定期检查报价是否已过期(结束日期 > 今天),为此我正在尝试使用 MySQL 计划事件:

CREATE EVENT IF NOT EXISTS check_expired_offers
    ON SCHEDULE EVERY 10 MINUTE
    DO
        BEGIN
            DECLARE id INT;
            DECLARE end_date DATE;
            DECLARE offer_cursor CURSOR FOR SELECT id, end_date FROM fouras.offer;

            OPEN offer_sursor;
            offer_loop: LOOP
                FETCH offer_cursor into id, end_date;
                IF end_date < NOW() THEN 
                    UPDATE fouras.offer set is_active = false;  
                END IF;

            END LOOP
END;

但是当我尝试添加这个事件时 MySQL 抛出一个错误:

Error Code: 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 '' at line 5    0,127 sec

最佳答案

错误是关于分隔符的,这个tutorial对我帮助很大

关于mysql - 在 MySQL 中创建计划事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187426/

相关文章:

mysql - 选择条件(所有数据都有这个条件)Sql

mysql - 从多个表中进行选择,其中结果在另一个表上出现的数量少于 X 数量

c# - DLL 中的 SqlConnection 对象在实例化时抛出 "Keyword not supported"异常

php - MySQL 触发器不会触发

PHP 和 Mysql 查询 JSON 输出

php - SELECT/UPDATE 和 SELECT/INSERT 杀死 MySQL 服务器

mysql - 跟踪 MySQL 中最后读取的记录

mysql - 使用 mySQL Workbench 向用户添加表权限

php - 尝试为用户显示@mentions,但我的查询出现错误

javascript - 使用 tedious(Node.js) 执行 SQL 查询