MySQL触发器删除相同的ID

时间:2018-06-20 08:37:47

标签: mysql triggers

使用TRIGGER,我想删除table2中具有与id在table1中删除的行相同的ID的行

CREATE TRIGGER trgDeleteInTable2 AFTER DELETE ON table1
FOR EACH ROW
BEGIN
DELETE  FROM table2 a  WHERE a.id =  the same id than the one deleted in table1 
END;

我该怎么办?如何获取表1中删除的当前ID?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

尝试一下:

CREATE TRIGGER trgDeleteInTable2 AFTER DELETE ON table1
FOR EACH ROW
BEGIN
DELETE * FROM table2 a  WHERE a.id =  OLD.id 
END;

OLD.id是已被删除的id中的table1