我已经使用MySQL一段时间了,但是我对MySQL触发器是完全陌生的。
我有这个数据库:
jours(jours(日期(主键),ventes(整数),soldeinitial(int)));
achatscharges(Id(int,主键),libelle(varchar),prix(int),#jour(日期,forein键)。
在我的表上每次插入或更新(“ achatscharges”)之后,触发器应检查表“ jours”中的“ soldeinitial”值是否为空,然后使用以下公式对其进行更新:
'类似于(最后一行中'jours.soldeinitial'的值
'jours'-sum(achatscharges.prix)+ jours.ventes'
这是我尝试过的:
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
这是例子:
+------------+-------- +---------------------+
| 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 |
+------------+---------+---------------------------+
请原谅我讲得不好,这就是例子
+------------+-------- +-------------------------+
| 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 |
+------------+---------+---------------------+
希望能澄清我的注意事项。
最佳答案
我已经处理了这个测试数据:
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;
希望能有所帮助。更新触发器类似。