[英]MySql Trigger on Insert, Update
I have been using MySQL for some time but I am totally new to MySQL triggers. 我已经使用MySQL一段时间了,但是我对MySQL触发器是完全陌生的。
I have this database : 我有这个数据库:
After each INSERT OR UPDATE on my table (' achatscharges ') the trigger should check if ' soldeinitial ' value in table ' jours ' is null then update it with this formula : 在我的表上执行每个INSERT OR UPDATE(“ achatscharges ”)之后,触发器应检查表“ jours ”中的“ soldeinitial ”值是否为空,然后使用以下公式对其进行更新:
'Something like( the value of ' jours.soldeinitial ' of the last row in ' jours ' - sum(achatscharges.prix) + jours.ventes '
'类似于(' jours '中最后一行的' jours.soldeinitial '值-sum (achatscharges.prix) + jours.ventes '
This is what I have tried : 这是我尝试过的:
CREATE TRIGGER UpdateSolde
AFTER INSERT, UPDATE ON achatscharges
FOR EACH ROW
BEGIN
IF((SELECT jours.soldeinitial FROM jours WHERE jours.jour=New.jour) IS NULL)
THEN
UPDATE jours SET jours.soldeinitial=((SELECT jours.soldeinitial FROM jours WHERE jours.soldeinitial IS NOT NULL ORDER BY jours.jour DESC LIMIT 1)-SUM(New.prix)+jours.ventes) WHERE jours.jour=New.jour);
END IF;
END
This is example : 这是例子:
+------------+-------- +---------------------+
| jour | ventes | soldeinitial |
+------------+---------+---------------------+
| 11-09-2017 | 1500 | 3000 |
| 12-09-2017 | 2000 | (must be calculated)|
| 13-09-2017 | 4000 | (must be calculated)|
+------------+---------+---------------------+
+------------+-------- +---------------------+
| id | libelle | prix | #jour |
+------------+---------+---------------------+
| 1 | somthing | 300 | 12-09-2017 |
| 2 | somthing | 200 | 12-09-2017 |
| 3 | somthing | 800 | 13-09-2017 |
+------------+---------+---------------------+
after triggger insert or update in achatscharges :
i should have this result :
+------------+-------- +---------------------------+
| jour | ventes | soldeinitial |
+------------+---------+---------------------------+
| 11-09-2017 | 1500 | 3000 |
| 12-09-2017 | 2000 | 4500 =(3000-(300+500)+2000|
| 13-09-2017 | 4000 | 7700 =4500-(800)+4000 |
+------------+---------+---------------------------+
excuse me to not explain very well,this is the example 请原谅我讲得不好,这就是例子
+------------+-------- +-------------------------+
| jour | ventes | soldeinitial |
+------------+---------+-------------------------+
| 11-09-2017 | 1500 | 3000 manually |
| 12-09-2017 | 2000 | 4500 calculated |
| 13-09-2017 | 4000 | 7700 calculated |
| 14-09-2017 | 3000 | *3000* manually |
| 15-09-2017 | 1500 | 3700 = *3000*-(800)+1500| the trigger doesn't work here the value still null
+------------+---------+-------------------------+
+------------+-------- +---------------------+
| id | libelle | prix | #jour |
+------------+---------+---------------------+
| 1 | somthing | 300 | 12-09-2017 |
| 2 | somthing | 200 | 12-09-2017 |
| 3 | somthing | 800 | 13-09-2017 |
| 4 | somthing | 800 | 15-09-2017 |
+------------+---------+---------------------+
Hope that clarify what I noted. 希望能澄清我的注意事项。
CREATE TABLE jours
(`jour` DATE, `ventes` int, `soldeinitial` int)
;
CREATE TABLE achatscharges
(`id` int, `libelle` varchar(8), `prix` int, `jour` DATE)
;
INSERT INTO jours
(`jour`, `ventes`, `soldeinitial`)
VALUES
('2017-09-11', 1500, 3000),
('2017-09-12', 2000, null),
('2017-09-13', 4000, null)
;
DELIMITER //
DROP TRIGGER IF EXISTS InsertSolde//
CREATE TRIGGER InsertSolde
AFTER INSERT ON achatscharges
FOR EACH ROW
BEGIN
DECLARE l_previous INTEGER;
DECLARE l_sumprix INTEGER;
IF EXISTS(SELECT jour FROM jours WHERE jour = NEW.jour)
THEN
SET l_previous = (SELECT soldeinitial FROM jours WHERE soldeinitial IS NOT NULL AND jour < NEW.jour ORDER BY jour DESC LIMIT 1);
SET l_sumprix = (SELECT SUM(prix) FROM achatscharges WHERE jour = NEW.jour GROUP BY jour);
UPDATE jours SET soldeinitial = l_previous-l_sumprix+ventes WHERE jour=New.jour;
END IF;
END //
DELIMITER ;
INSERT INTO achatscharges
(`id`, `libelle`, `prix`, `jour`)
VALUES
(1, 'somthing', 300, '2017-09-12');
SELECT * FROM jours;
INSERT INTO achatscharges
(`id`, `libelle`, `prix`, `jour`)
VALUES
(2, 'somthing', 200, '2017-09-12');
SELECT * FROM jours;
INSERT INTO achatscharges
(`id`, `libelle`, `prix`, `jour`)
VALUES
(3, 'somthing', 800, '2017-09-13')
;
SELECT * FROM jours;
Hope that helps. 希望能有所帮助。 An update trigger is similar.
更新触发器类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.