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

标签 mysql events triggers

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


-programmed_id int
-interval int//以天为单位或以更容易使用的方式为单位

-数量 float

-product int a foregin key

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

我需要第二个触发器来删除事件并使用新变量显然在更新时重新创建它

删除时我只有一个问题,我如何根据删除的行知道要删除哪个事件?

问题是我刚刚意识到您无法处理触发器内的事件。有人知道解决方法吗?也许是一个 php 解决方案?有什么建议吗?

非常感谢您的帮助。

过了一会儿....

这行得通吗??作为做我想做的事情的测试

每天打折送货

创建数据库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 on t22.id=t1.id_producto set t1.quantity=t1.quantity -t22.quantity 其中 t1.id_producto>0;

最佳答案

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

事实上,不可能从触发器管理事件,但单个“主”事件可以定期和频繁地运行(甚至每分钟),它会检查 event 以获取某些东西做。

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

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

关于mysql - 插入时需要 mysql 事件的最佳解决方案解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715263/

相关文章:

mysql - 对所有属性使用 distinct

SQL Server 2008 - 帮助编写简单的 INSERT 触发器

wpf - 如何使TreeViewItem上的IsMouseOver的WPF触发器不影响moused-over控件的所有父级?

postgresql - 我该如何优化这个 : Many Postgres triggers calling the same function within a transaction

mysql - SELECT 返回未出现在任何行中的值列表

mysql - 在查询中计算 GPA

mysql - 删除一行后重新调整 auto_increment 主键字段

java - 输入文本字段不会在单击鼠标时移动光标?

javascript - 我应该在 jQuery 中的哪里绑定(bind)全局事件

javascript - "request"和 "response"是从哪里来的,我怎么知道的?