我尝试了以下三个陈述,并且不确定如何将它们组合起来:
CREATE TRIGGER upd_totals
AFTER INSERT ON pc12_status
FOR EACH ROW
INSERT INTO total_hrs (acft_hrs, date_flight, acft_tail)
SELECT hrs_end, flight_date, tail_no FROM acft_status
INNER JOIN total_hours ON acft_status.tail_no = total_hrs.acft_tail;
&安培;
CREATE TRIGGER total_calc_prop
AFTER INSERT ON total_hrs
FOR EACH ROW
UPDATE total_hrs SET prop_hrs = (SELECT s.flight_date, s.tail_no, s.flight_hrs
SUM(s.flight_hrs) OVER (PARTITION BY s.tail_no) AS prop_hrs
FROM acft_status s);
&安培;
CREATE TRIGGER total_calc_eng
AFTER INSERT ON total_hrs
FOR EACH ROW
UPDATE total_hrs SET engine_hrs = (SELECT s.flight_date, s.tail_no, s.flight_hrs
SUM(s.flight_hrs) OVER (PARTITION BY s.tail_no) AS engine_hrs
FROM acft_status s);
我有以下两个数据表
acft_status
flight_date flight_hrs tail_no hrs_end hrs_start
2017-12-31 100.0 1378 100.0 0.0
total_hrs
date _flight acft_tail acft_hrs engine_hrs prop_hrs
2017-12-31 1378 100.0 98.5 96.5
无论何时我向acft_status表添加数据,我都需要flight_hrs自动与engine_hrs和prop_hrs字段中的前一个条目求和,同时将hrs_end复制到acft_hrs字段:
INSERT INTO acft_status (flight_date, flight_hrs, tail_no, hrs_end, hrs start)
VALUES ("2018-01-01", 1.3, 1378, 101.3, 100.0);
然后将total_hrs更新为:
total_hrs
date _flight acft_tail acft_hrs engine_hrs prop_hrs
2017-12-31 1378 100.0 98.5 96.5
2017-12-31 1378 101.3 99.8 97.8
当我尝试运行所有三个触发器时,我会收到"无法更新表格' total_hrs'在存储的函数/触发器中,因为它已被调用此存储的函数/触发器的语句使用。"。
我正在运行MariaDB 10.2,它允许将FOLLOW
和PROCEED
添加到触发器中。据说,它允许您在一个事件上运行多个触发器。
触发器是最有效的方法吗?如果是这样,我如何将它们组合起来自动运行?我应该尝试MERGE
而是使用php ????