无法在触发器中设置变量

时间:2014-09-16 23:59:45

标签: mysql triggers phpmyadmin declaration

我正在尝试在触发器中设置一个变量,该触发器是表中最新的条目。但是,PHPMyAdmin注意到行SET clicked_campaign_id =出错。我在这里没有看到问题。

CREATE TRIGGER tr_user_action_click 
AFTER INSERT ON users_click FOR EACH ROW
BEGIN
    DECLARE clicked_campaign_id int

    SET clicked_campaign_id = 
    (SELECT campaignId 
    FROM users_click 
    WHERE id = (SELECT max(id) FROM users_click));

    Update onlineportal.`campaigns`
    SET `clicks` = `clicks` + 1
    WHERE id = clicked_campaign_id;
END

有没有不同的方法来设置变量......?

1 个答案:

答案 0 :(得分:1)

您必须在phpMyAdmin中设置DELIMITER,如图所示:

enter image description here

然后,创建触发器:

CREATE TRIGGER `tr_user_action_click` AFTER INSERT ON `users_click`
FOR EACH ROW
BEGIN
    DECLARE `clicked_campaign_id` INT;

    SET `clicked_campaign_id` = 
    (SELECT `campaignId` 
    FROM `users_click`
    WHERE `id` = (SELECT max(`id`) FROM `users_click`));

    UPDATE `onlineportal`.`campaigns`
    SET `clicks` = `clicks` + 1
    WHERE `id` = `clicked_campaign_id`;
END//