【问题标题】:Creating an "After Insert" Trigger创建“插入后”触发器
【发布时间】:2014-09-20 15:14:55
【问题描述】:

请看下图

我需要在Ongoing_Portfolio 中编写一个触发器,它会在Insert 操作后自动更新Ongoing_Fees

Portfolio.Other_Fee 是一个百分比,而Ongoing_Fees.Other_Fee = Ongoing_Portfolio.Cash_Value * Portfolio.Other_Fee

请注意所有 3 个表格中的 idPortfolio。这里发生的是在用户将数据插入Ongoing_Portfolio 之后,它使用idPortfolio 扫描相关的Portfolio。然后它从相关投资组合中抓取Other_Fee 并将数据插入到 Ongoing_Fees 中。

下面是我写的Trigger,但是我写不了插入方法来做Other_Fee

CREATE TRIGGER `Ongoing_Portfolio_AINS` AFTER INSERT ON `Ongoing_Portfolio` FOR EACH ROW
INSERT INTO Ongoing_Fees (idPortfolio,Other_Fee)
VALUES
(New.idPortfolio, )

我怎样才能完成这个触发器?

示例:

Portfolio.idPortfolio = 1
Portfolio.Other_Fee = 10%

现在我将数据插入Ongoing_Portfolio

Ongoing_Portfolio.idPortfolio = 1
Ongoing_Portfolio.Cash_Value = 1000

现在,trigger 已经触发,所以 Ongoing_Fees 中的数据应该是

Ongoing_Fees.idPortfolio = 1
Ongoing_Fees.Other_Fee = 100

【问题讨论】:

  • 你能举个例子说明在将一个例子插入到 progress_portfolio 之后应该发生什么效果
  • 请考虑确切的数据类型 DECIMAL 的费用,而不是像 DOUBLE 这样的近似数据类型。
  • @BrianDeMilia:完成。请查看已编辑的问题

标签: mysql sql database triggers insert


【解决方案1】:

这是你需要的吗?

INSERT INTO Ongoing_Fees (idPortfolio,Other_Fee)
    select New.idPortfolio, p.other_fee
    from Portfolio p
    where p.idPortfolio = New.idPortfolio;

【讨论】:

  • 感谢您的回复..但是,errr..Values 部分在哪里?
  • @Sniper 那是(INSERT INTO ... SELECT 语法)[dev.mysql.com/doc/refman/5.6/en/insert-select.html].不需要VALUES
  • @Sniper 。 . . insert 不需要 Values。事实上,insert . . . select 做了所有 insert . . . values 所做的事情,甚至更多。
  • err..好吧,其实你的p.other_fee应该是New.Cash_Value * p.Other_Fee吧? (如果您感到困惑,可以查看我更新的问题,其中有一个示例......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
相关资源
最近更新 更多