[英]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.