在插入时需要mysql事件的最佳解决方案?

时间:2014-12-31 02:06:42

标签: mysql events triggers

我将一个已编程的交付插入到sql中,这是一个由以下内容组成的表: 最佳解决方案:


-programmed_id int  
-interval int //在几天内或者什么时候更容易使用

数量浮标
 
-product int in foregin key

我想创建三个在insert上运行的触发器,以便它们创建一个事件,该事件在每个时间间隔(在新行中指定)运行,更新产品设置products.quantity = products.quantity - programmed_delivery.quantity where products。 ID =产品

我需要第二个触发器来删除事件,并在更新时使用新变量

显然重新创建它

在删除时,我只遇到一个问题,我怎么知道要删除哪个事件取决于删除哪一行?

事情是我刚刚意识到你无法处理触发器内的事件。有人知道解决方法吗?也许一个PHP解决方案?建议?

非常感谢你的帮助。

过了一会......

这会有用吗?作为做我想做的事的测试

每日扣除交货

创建数据库prueba;   使用prueba;  创建表t1(id_producto int(11)not null auto_increment,quantity int(11)not null,primary key(id_producto));   创建表t2(id_delivery int(11)not null auto_increment,id_producto int(11)not null,quantity int(11)not null,fecha date not null default' 2014-12-11',primary key( id_delivery));   插入t1(数量)值(5),(6),(8);   插入到t2(id_producto,quantity,fecha)值(1,1,curdate()),(2,2,curdate()),(1,2,curdate());    创建事件如果不存在每1天按计划交付,请执行更新t1内部联接(选择id_producto作为id,t2中的数量,其中fecha = curdate())t22,t22.id = t1.id_producto set t1.quantity = t1.quantity -t22.quantity其中t1.id_producto> 0;

1 个答案:

答案 0 :(得分:0)

要解决从event表更新或删除相关条目的问题,只需为programmed_delivery表创建一个foregin密钥。

确实无法从触发器管理事件,但是单个“主”事件可以定期和频繁地运行(甚至每分钟),这会检查event要做的事情。< / p>

programmed_delivery表上的触发器将更新此自定义event表。

但您可能不需要这样的event表。 “主事件”可以检查programmed_delivery表以确定此时是否应该更新库存。