如何根据 mysql 中以前的条目更新表

发布于 2024-08-27 09:09:00 字数 420 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-09-03 09:09:00

它是否必须是“插入后一个”触发器?如果您需要 auto_increment 值来计算要修改的字段的值,就会出现这种情况。否则,应该使用“插入之前”触发器。您可以通过SET NEW.fieldname = newvalue更改要插入的字段的值,

例如,让我们看一下表

CREATE TABLE foo (id int auto_increment, x int, y int, primary key(id))

创建一个插入前触发器

delimiter |

CREATE TRIGGER footrigger BEFORE INSERT ON foo
  FOR EACH ROW BEGIN
    IF NEW.y > NEW.x THEN
       SET NEW.y = -1;
    END IF;
  END;
|

现在让我们插入两条记录,一条记录 y=x,一条记录 y>x

INSERT INTO foo (x,y) VALUES (1,1), (1,2)

A SELECT x,y FROM foo 然后返回

"x";"y"
"1";"1"
"1";"-1"

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

CREATE TABLE foo (id int auto_increment, x int, y int, primary key(id))

and create a before insert trigger

delimiter |

CREATE TRIGGER footrigger BEFORE INSERT ON foo
  FOR EACH ROW BEGIN
    IF NEW.y > NEW.x THEN
       SET NEW.y = -1;
    END IF;
  END;
|

And now let's insert two records, one where y=x and one where y>x

INSERT INTO foo (x,y) VALUES (1,1), (1,2)

A SELECT x,y FROM foo then returns

"x";"y"
"1";"1"
"1";"-1"
墟烟 2024-09-03 09:09:00

直接改插入机制不行吗?
插入表(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_','')...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文