在mysql中创建事件删除行

时间:2018-03-24 13:58:28

标签: mysql events

如何在另一个表中更改值时自动删除行的事件? 我的表格看起来像这样:

tbl_people
+----+-------+----------+
| ID |  NAME |  STATUS  |
+----+-------+----------+
|  1 | name1 |  active  |
|  3 | name3 |  active  |
+----+-------+----------+

tbl_activities
+----+---------+-------+
| ID | ID_NAME |  NAME |
+----+---------+-------+
|  1 |    1    | name1 |
|  2 |    3    | name3 |
+----+---------+-------+

当在id为3的tbl_people时,它会在tbl_activities中从active变为inactive,以自动删除id为2的行

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用触发器

   CREATE TRIGGER bfr_upd_activity BEFORE UPDATE ON tbl_people
   FOR EACH ROW
   BEGIN
      if new.status <> 'active' then 
        delete from tbl_activities where id_name = new.id;
      end if;
   END;//