简体   繁体   English

MySQL触发错误:根据另一张表的更新或插入更新一个表的字段值

[英]MySQL Trigger Error: Updating the one table's field value based upon another table updates or insertions

I want to update one table in MySQL based upon another table update and insertions. 我想基于另一个表的更新和插入来更新MySQL中的一个表。 Scenario is like this, when someone change the status of the column in main table the child table's column field should be update for their matching ID. 场景是这样的,当有人更改主表中列的状态时,应针对其匹配ID更新子表的列字段。 Tables look like as below: 表格如下所示:

po_request 
Id  po_id   status
1   E0001   Requested
2   E0002   Received 

PO_LINE PO_LINE

Id po_id is_received 
6  E0001    0
7  E0002    0

Need to update the Po_line table each time when status has changed to "Received" OR directly insert the "received" in the table. 每次状态更改为“已接收”时都需要更新Po_line表,或者直接在表中插入“已接收”。 I have made trigger but it's not working. 我已经触发了,但是没有用。 Trigger 触发

DROP TRIGGER IF EXISTS `t1`;
DELIMITER $$
CREATE TRIGGER `t1`
AFTER UPDATE ON po_request FOR EACH ROW 
BEGIN 
IF NEW.`status` = 'Received' 
THEN 
UPDATE po_line JOIN po_request ON po_request.po_id = po_line.po_id SET is_received = '1' WHERE po_request.status = 'Received'; END IF;
END$$
DELIMITER ; 

Trigger loaded into table successfully but when I update the table it throws very weird error: 触发器成功加载到表中,但是当我更新表时会引发非常奇怪的错误:
Error Code: 1054. Unknown column 'date_received' in 'field list'. 错误代码:1054。“字段列表”中的未知列“ date_received”。

I think you need to update it by avoid joining the table and match in where clause with the reference of NEW keyword. 我认为您需要通过避免加入表来进行更新,而避免使用NEW关键字的引用在where子句中进行匹配。 which refers to the other table id like : 指的是其他表id例如:

UPDATE po_line 
SET po_line.is_received = '1' 
WHERE new.po_id = po_id 
AND po_request.status = 'Received';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于另一个表中的值设置一个表的字段 - How to set one table's field based on value in another table MySQL中嵌套的select语句用于根据另一个字段的最大值更新字段会产生错误 - Nested select statement in MySQL for updating field based upon max value of another field gives error MySQL触发器更新当前表中更新值的另一个表 - MySQL trigger for updating another table on updating value in current table 插入触发器后更新另一个表的列的mysql - mysql after insert trigger which updates another table's column 根据另一个表中的值更新一个表中的MySQL行 - Update a MySQL row in one table depending upon a value in another table MySQL根据另一个表中同一行的另一个字段值的重复外观更新布尔值 - MySQL updating a Boolean field value based on the duplicated appearance of another filed value of the same row from another table MYSQL:根据与另一个表的字段的部分匹配来更新客户字段 - MYSQL: Updating a customer field based on a partial match to another table's field 根据使用另一个表中的变量进行的计算更新一个mysql表 - Updating one mysql table based on calculations using variables in another table MySQL触发器,用于将一个表的行聚合到另一个表 - MySQL Trigger for aggregating one table's rows to another table MySQL:从另一个表中更新一个表中的id值 - MySQL: Updating id value in one table from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM