Mysql 删除带条件的触发器中的语法错误

Syntax Error in Mysql Delete Trigger with conditions

我在MySQL中写了一个小的触发函数。这是我写的触发器查询,在代码中给出了语法错误。

错误:

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 'cp WHERE cp.customer_plan_id= first_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 ;

来自 MySQL documentationDELETE 查询的语法是:

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 变量的默认值是一个好习惯。例如:

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