mysql delete trigger with conditions语法错误

bvhaajcl  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(286)

我在mysql中编写了一个小的触发器函数。这是我编写的触发器查询,并在代码中给出语法错误。
错误:
sql语法有错误;查看与mysql服务器版本对应的手册,以获得使用“cp where”附近的正确语法 cp . customer_plan_id =第一个id;

DELIMITER $$
CREATE TRIGGER delete_plan_on_delete_customer 
    AFTER DELETE ON customers
    FOR EACH ROW 
BEGIN
    DECLARE active_plan INT;
    DECLARE first_id INT;

    SET active_plan = (SELECT is_active
    FROM customer_plans 
    WHERE customer_id=OLD.customer_id AND first_plan_id=1);

    if(active_plan = 0)THEN

        SET first_id = (SELECT customer_plan_id
        FROM customer_plans 
        WHERE customer_id=OLD.customer_id AND first_plan_id=1);

        DELETE customer_plans cp WHERE `cp`.`customer_plan_id`= first_id;
   END IF; 
END$$
DELIMITER ;
5n0oy7gb

5n0oy7gb1#

的语法 DELETE mysql文档中的查询是:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

改变你的想法 DELETE 触发器中的行:

DELETE FROM customer_plans WHERE customer_plan_id = first_id;

旁注:根据我的个人经验,为变量声明默认值是一个好习惯。如:

DECLARE active_plan INT(11) DEFAULT 0;
DECLARE first_id INT(11) DEFAULT 0;

相关问题