2017-05-19 81 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