MySQL 触发器:使用另一个 table 的值

MySQL trigger: Using value from another table

我正在使用 MYSQL 8.0.18.

我有三个 table:贸易、投资组合和 closed_trades。

我正在尝试在交易 table 更新后创建一个触发器,它将在 closed_trades table 上插入值。在此插入任务中 average_price 来自投资组合 table 的价值也需要插入已关闭的交易 table 中。但我无法获得该值

DELIMITER $$
CREATE TRIGGER closed_trade_update
    after UPDATE ON trade for each row
    BEGIN
        IF NEW.action = 'sell'
            THEN
                INSERT INTO closed_trades
                SET ticker = NEW.ticker,
                    quantity = NEW.quantity,
                    sell_price = NEW.real_price,
                    avg_cost = portfolio.average_price;

            END IF;
    End;
$$
DELIMITER ;

我收到此错误消息“'field list' 中的未知列 'portfolio.average_price'”

我的 table

的详细信息
DESC trade;

+-------------+---------------------+------+-----+-------------------+-------------------+
| Field       | Type                | Null | Key | Default           | Extra             |
+-------------+---------------------+------+-----+-------------------+-------------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL              | auto_increment    |
| ticker      | varchar(25)         | NO   | MUL | NULL              |                   |
| action_date | datetime            | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
| action      | enum('buy','sell')  | NO   |     | NULL              |                   |
| basic_price | decimal(10,2)       | NO   |     | NULL              |                   |
| real_price  | decimal(10,2)       | YES  |     | NULL              |                   |
| quantity    | decimal(10,0)       | YES  |     | NULL              |                   |
| total       | decimal(10,0)       | YES  |     | NULL              |                   |
+-------------+---------------------+------+-----+-------------------+-------------------+
DESC portfolio;

+---------------+---------------+------+-----+---------+-------+
| Field         | Type          | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| ticker        | varchar(25)   | NO   | PRI | NULL    |       |
| quantity      | decimal(10,0) | YES  |     | NULL    |       |
| average_price | decimal(10,2) | YES  |     | NULL    |       |
| value         | decimal(10,2) | YES  |     | NULL    |       |
+---------------+---------------+------+-----+---------+-------+
DESC closed_trades;
+-----------------+---------------------+------+-----+------------------------------------------------------------+-------------------+
| Field           | Type                | Null | Key | Default                                                    | Extra             |
+-----------------+---------------------+------+-----+------------------------------------------------------------+-------------------+
| id              | bigint(20) unsigned | NO   | PRI | NULL                                                       | auto_increment    |
| ticker          | varchar(25)         | NO   | MUL | NULL                                                       |                   |
| action_date     | datetime            | YES  |     | CURRENT_TIMESTAMP                                          | DEFAULT_GENERATED |
| sell_price      | decimal(10,2)       | YES  |     | NULL                                                       |                   |
| avg_cost        | decimal(10,2)       | NO   |     | NULL                                                       |                   |
| quantity        | decimal(10,0)       | YES  |     | NULL                                                       |                   |
| total_received  | decimal(10,2)       | YES  |     | (`sell_price` * `quantity`)                                | DEFAULT_GENERATED |
| total_cost      | decimal(10,2)       | YES  |     | (`avg_cost` * `quantity`)                                  | DEFAULT_GENERATED |
| capital_gain    | decimal(10,2)       | YES  |     | (`total_received` - `total_cost`)                          | DEFAULT_GENERATED |
| gain_percentage | decimal(10,3)       | YES  |     | (((`total_received` - `total_cost`) / `total_cost`) * 100) | DEFAULT_GENERATED |
+-----------------+---------------------+------+-----+------------------------------------------------------------+-------------------+

请帮助我完成我的项目。我会很感激你的。

我想你想要 insert ... select :

insert into closed_trades (ticket, quantity, sell_price, avg_cost)
select new.ticker, new.quantity, new.real_price, p.average_price
from portfolio p
where p.ticker = new.ticker

如果更新行的 ticker 可能不存在于 portfolio 中,而您仍然希望在 closed_trades 中插入一行 [=16] =] 中的值 avg_cost,您可以使用子查询代替:

insert into closed_trades (ticket, quantity, sell_price, avg_cost)
values (
    new.ticker, 
    new.quantity, 
    new.real_price, 
    (select p.average_price from portfolio p where p.ticker = new.ticker)
)