如何根据 mysql 中以前的条目更新表
我在 mysql 中使用触发器时遇到了严重的问题。 基本上我试图在插入后更新一行。我正在使用该行中的现有值并尝试更新同一行的其他列。
例如:
Existing_Name Actual_Name
Product_Arc1 Arc1
Product_Arc2 Arc2
Product_Arc3
我正在获取 Existing_Name
的值并提取它的第二部分,并使用触发器更新 Actual_Name
。但是既然 MySQL 不支持更新激活触发器的同一个表呢?有什么可能的方法来做我想做的事吗?
截至目前,我正在使用 PHP 脚本来执行相同的效果。但这要求最终用户在数据库发生变化时不断从 Web 浏览器触发此脚本。
任何想法表示赞赏。
谢谢。
I am having a serious trouble using triggers in mysql.
Basically I am trying to update a row on after insert
. I am using the existing value in the row and trying to update an other column of same row.
Ex:
Existing_Name Actual_Name
Product_Arc1 Arc1
Product_Arc2 Arc2
Product_Arc3
I am taking the value of Existing_Name
and extracting the second part of it and updating the Actual_Name
using triggers. BUt since MySQL does not support updating the same table the trigger was activated on? is there any possible way to do what I want?
As of now, I am using PHP script to perform the same effect. But this requires the end user to keep triggering this script from the web browser whenever there are some changes in the database.
Any ideas appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是否必须是“插入后一个”触发器?如果您需要 auto_increment 值来计算要修改的字段的值,就会出现这种情况。否则,应该使用“插入之前”触发器。您可以通过
SET NEW.fieldname = newvalue
更改要插入的字段的值,例如,让我们看一下表
和创建一个插入前触发器
现在让我们插入两条记录,一条记录 y=x,一条记录 y>x
A
SELECT x,y FROM foo
然后返回Does it have to be an "one after insert" trigger? This would be the case if you needed an auto_increment value to calculate the value of the field you want to modify. Otherwise a "before insert" trigger should do. You can change values of fields to be inserted via
SET NEW.fieldname = newvalue
E.g. let's take the table
and create a before insert trigger
And now let's insert two records, one where y=x and one where y>x
A
SELECT x,y FROM foo
then returns直接改插入机制不行吗?
插入表(Existing_Name,Actual_Name)值($Existing_Name,替换($Existing_Name,'Product_','')...
Can't you directly change the insert mechanism?
insert into table(Existing_Name,Actual_Name) values ($Existing_Name, replace($Existing_Name,'Product_','')...