2014-09-20 146 views
0

請看看下面的圖創建「後插入」觸發器

enter image description here

我需要寫一個觸發器在Ongoing_PortfolioInsert操作後自動更新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。

下面是我寫的觸發器,但我無法編寫插入方法來執行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 
+0

你能舉一個例子後,應該會出現什麼效果插入ongoing_portfolio – 2014-09-20 15:24:45

+0

請考慮收費,而不是近似的像'DOUBLE'確切的數據類型'DECIMAL'的一個例子。 – VMai 2014-09-20 15:30:00

+0

@BrianDeMilia:完成。請參閱已編輯的問題 – 2014-09-20 15:44:51

回答

3

是這是你什麼ED?

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

感謝您的回覆。但是,錯誤。「值」部分在哪裏? – 2014-09-20 15:39:13

+0

@Sniper這是(INSERT INTO ... SELECT語法)[https://dev.mysql.com/doc/refman/5.6/en/insert-select.html]。不需要'VALUES'。 – VMai 2014-09-20 15:41:36

+0

@Sniper。 。 。 'insert'不需要'Values'。事實上,'插入。 。 。選擇'做一切'插入。 。 。價值觀',還有更多。 – 2014-09-20 15:43:57