简体   繁体   English

插入删除查询后MySQL触发器不起作用

[英]MySQL trigger after insert delete query not working

I have the following trigger, created in phpMyAdmin: 我在phpMyAdmin中创建了以下触发器:

Table: tb_agenda Time: AFTER Event: INSERT

BEGIN
    DECLARE x INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;

    SELECT COUNT(*) FROM tb_agenda INTO x;

    SET i = 0;

    WHILE i < x DO
        INSERT INTO tb_realizacao (dt_agenda,
                                   titulo, 
                                   titulo_en, 
                                   descricao, 
                                   descricao_en, 
                                   dt_cadastro)
        SELECT  dt_agenda,
                titulo,
                titulo_en,
                descricao,
                descricao_en,
                dt_cadastro
        FROM tb_agenda
        WHERE dt_agenda < NOW();

        DELETE FROM tb_agenda
         WHERE dt_agenda < NOW();

        SET i = i + 1;
    END WHILE;
END

What's this: after inserting into tb_agenda , it's supposed to search for data which dt_agenda (date) is lower than NOW() , add into tb_realizacao and then delete this old data from tb_agenda . 这是什么:插入tb_agenda ,应该搜索dt_agenda (日期)小于NOW() ,添加到tb_realizacao ,然后从tb_agenda删除此旧数据。 The problem is that the delete query seems not to be executed, the data is normally added into the tb_realizacao but not deleted from tb_agenda . 问题在于删除查询似乎未执行,通常将数据添加到tb_realizacao但未从tb_agenda删除tb_agenda

No need to set WHERE since NOW means that all of them will be removed. 无需设置WHERE,因为NOW意味着将全部删除。 No need for WHILE since every trigger has FOR EACH ROW. 无需WHILE,因为每个触发器都有FOR EACH ROW。 You cannot delete from table where you insert so delete will be in the php script. 您不能从插入表中删除,因此删除将在php脚本中进行。

php script: php脚本:

$pdo->query("DELETE FROM tb_agenda");
$pdo->query("INSERT ...");

trigger: Table: tb_agenda Time: BEFORE Event: DELETE 触发器:表:tb_agenda时间:在事件之前:删除

BEGIN
    INSERT INTO tb_realizacao (dt_agenda,
                               titulo, 
                               titulo_en, 
                               descricao, 
                               descricao_en, 
                               dt_cadastro)
    VALUES  (OLD.dt_agenda,
            OLD.titulo,
            OLD.titulo_en,
            OLD.descricao,
            OLD.descricao_en,
            OLD.dt_cadastro);

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM