考虑表 TRADE_ORDERS
。
Symbol | Type | Count | Settled | Settled_count
------ | -----| ------| --------| -------------
Apple | BUY | 100 | NO | 0
Apple | BUY | 50 | NO | 0
我写了一个触发器after_trade_orders_insert
,它会AFTER INSERT
新行将根据是否有未结算的订单来尝试结算之前的订单。
现在,如果我将 INSERT
到 TRADE_ORDERS
值为 Apple, SELL, 150
的行,它应该结算所有购买的股票。因此,预期的操作应该使表格处于以下状态。
预期输出
Symbol | Type | Count | Settled | Settled_count
------ | -----| ------| --------| -------------
Apple | BUY | 100 | YES | 100
Apple | BUY | 50 | YES | 50
Apple | SELL | 150 | YES | 150
我明白了如何在mysql中写代码来实现。但是当我在 after_trade_orders_insert
中编写代码时,它失败并出现错误
Error Code: 1442. Can't update table 'NSE_FO' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 0.055 sec
我明白这个错误是什么以及为什么会发生。
问题:如何实现预期的输出?
最佳答案
您不能通过触发器更新表中最近插入的行,因此您必须使用带有 new 关键字的 where 子句。
关于MySQL 错误 1442 : Cannot update a table in a trigger/stored procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445116/