我已经使用MySQL一段时间了,但我对MySQL触发器完全不熟悉。
我有这个数据库:
在我的桌子上每次插入或更新后(' achatscharges '),触发器应检查' soldeinitial '表格中的价值' jours '为null,然后使用以下公式更新它:
'某些事情(' 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 |
+------------+---------+---------------------+
希望澄清我所说的内容。
答案 0 :(得分:0)
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;
希望有所帮助。更新触发器类似。