2017-05-19 133 views
0

我想創建一個觸發器,其中 第一個值是從entry_table 計算我已經建立的查詢,以計算MySQL的觸發器的phpmyadmin

SELECT *, COUNT(DISTINCT account), SUM(amount) FROM entry GROUP BY account 

的值,然後插入計值稱爲total_table

條目表另一個表

enter image description here

總計表enter image description here

下了扳機查詢

INSERT INTO totals (SUM_A, SUM_B, SUM_C) 

SELECT *, COUNT(DISTINCT account), SUM(amount) FROM entry GROUP BY account 
+0

爲什麼要一次又一次地計算所有的值?並將它們作爲重複項插入到'totals'中? – Shaharyar

+0

我想更新錯誤我寫入插入 – Raahull

回答

0

這觸發對您有所幫助。

DROP TRIGGER IF EXISTS `enter_value`; 
DELIMITER ;; 
CREATE TRIGGER `enter_value` AFTER INSERT ON `entry_table` 
FOR EACH ROW begin 

    IF NEW.account = 'sum_a' then 
     INSERT INTO totals table (SUM_A) 
     SELECT sum(amount) FROM entry where account = 'sum_a' GROUP BY account; 
    ELSE IF NEW.account = 'sum_b' then 
     INSERT INTO totals table (SUM_B) 
     SELECT sum(amount) FROM entry where account = 'sum_b' GROUP BY account; 
    ELSE IF NEW.account = 'sum_c' then 
     INSERT INTO totals table (SUM_C) 
     SELECT sum(amount) FROM entry where account = 'sum_c' GROUP BY account; 
    END IF; 

END 

如果你取任何問題,讓我知道

+0

您的SQL語法有錯誤;檢查對應於您的MariaDB服務器版本的手冊,以在'=='sum_a附近使用正確的語法,然後 INSERT INTO總計表(SUM_A) SELECT sum(amoun'at line 4 – Raahull

+0

@Raahull use single'='instead '=='。 – Shaharyar

+0

我試過但沒有工作@ Shaharyar – Raahull