我无法在触发器中执行两个语句

时间:2019-06-04 09:03:05

标签: mysql sql triggers

我希望两个语句都可以在触发器中执行。我无法执行以下操作:

IF (flag = 'D') THEN
BEGIN
     INSERT INTO logs (id, author_id, action_done, description, old_value, new_value, create_date) 
     VALUES (null, (SELECT id FROM gallery WHERE flag = 'D'), 'Delete', 'Gallery', (SELECT filename FROM gallery WHERE flag = 'D'), '', NOW());
     DELETE FROM gallery WHERE flag = 'D';
END
END IF

3 个答案:

答案 0 :(得分:0)

我假设日志表中的id字段是主键,并且由系统自动生成。

尝试一下:

IF (flag = 'D') THEN
BEGIN
     INSERT INTO logs (author_id, action_done, description, old_value, new_value, create_date) 
     select id, 'Delete', 'Gallery', filename, '', NOW() from gallery where flag='D'; 
     DELETE FROM gallery WHERE flag = 'D';
END
END IF

答案 1 :(得分:0)

您的ID应该自动递增,因此不应该是其中之一。

答案 2 :(得分:0)

大概您的触发器在表gallery上。如果是这样,则您的触发器应该是“之前”或“之后”触发器,并且您可以执行以下操作:

IF (flag = 'D') THEN
BEGIN
     INSERT INTO logs (author_id, action_done, description, old_value, new_value, create_date) 
         VALUES (old.id, 'Delete', 'Gallery', old.filename, '', NOW());    
END;
END IF

无需重复DELETE