从不同表格中的另一列中减去一列,将答案留在 MySQL 中的另一列中

Subtracting one column from another column in different tables to leave the answer in another column in MySQL

m_type table membership table

我有会员资格 table 已付金额,应付金额。我还有一个 m_type table 的价格。 我想要一个触发器,这样当一行被插入或更新到成员 table WHERE membership.type_id = m_type.type-id 时,来自 m_type [=26= 的价格列] 从成员 table 中的 amount_paid 列中减去,答案被放入成员 table 中的 amount_due 列中。 谢谢

我目前尝试过的: What i have tried

IIUC:

DELIMITER $$

CREATE TRIGGER amount_due_cal BEFORE INSERT ON membership
  FOR EACH ROW
    BEGIN
    SET NEW.amount_due := (
        SELECT price FROM m_type WHERE type_id = New.type_id) - NEW.amount_paid;
END $$    
DELIMITER ;

如果这不起作用,请告诉我。

另请注意,type_id 必须是 price table 的唯一索引/主键,否则将不起作用。