我有一个想要的表:更新该表后,该表的2个字段(标题和描述)会发生变化,并从另一个表中获取价值
这是我的触发器:
drop trigger trigger_trade_request ; CREATE TRIGGER trigger_trade_request AFTER UPDATE ON `trade_request` FOR EACH ROW BEGIN IF NEW.title = null THEN UPDATE `trade_request_type`,`trade_request` SET NEW.title = `trade_request_type`.title , NEW.description = `trade_request_type`.description WHERE `trade_request_type`.id = NEW.trade_request_typeId; END IF; END;
我的表:
错误:
错误1442:无法更新存储函数/触发器中的表’trade_request’,因为调用该存储函数/触发器的语句已使用该表。
这没用!什么是概率?
is null
BEFORE
SET
NEW
试试这个:
CREATE TRIGGER trigger_update_request BEFORE UPDATE ON `trade_request` FOR EACH ROW BEGIN IF NEW.title IS NULL THEN SET NEW.title = (SELECT title FROM trade_request_type WHERE trade_request_type.id = NEW.trade_request_typeId); SET NEW.description = (SELECT description FROM trade_request_type WHERE trade_request_type.id = NEW.trade_request_typeId); END IF; END;
可能可以使用SELECT...INTO语法代替SET,如下所示:
SELECT...INTO
CREATE TRIGGER trigger_update_request BEFORE UPDATE ON `trade_request` FOR EACH ROW BEGIN IF NEW.title IS NULL THEN SELECT title, description FROM trade_request_type WHERE trade_request_type.id = NEW.trade_request_typeId INTO NEW.tile, NEW.description; END IF; END;