1

I have a table summary. I want a TRIGGER to fire before insert on this table i.e summary and what I want is that, the TRIGGER should check if the incoming date matches with existing date, it should update the table and add incoming value to existing value else it should just insert

`summary`
+------+-------+-------------+------+
| date |income | expenditure | other|
|22/17 | 200   |   50        |  30  |
|22/17 | 100   |   10        |  80  |
|23/17 | 50    |   100       |  0   |
+------+-------+-------------+------+

`summary` // this is how I want it to be
+------+-------+-------------+------+
| date |income | expenditure | other|
|22/17 | 300   |   150       |  30  |
|23/17 | 50    |   100       |  0   |
+------+-------+-------------+------+

my table code

CREATE TABLE `summary` (
  `id_` int(11) NOT NULL,
  `date_` date NOT NULL,
  `income_` text NOT NULL,
  `expenditure_` text NOT NULL,
  `other_` text NOT NULL,
  PRIMARY KEY(id) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `summary` 
(`id_`, `date_`, `income_`, `expenditure_`, `other_`)
VALUES
(1, '2017-12-22', 200, 50, 30),
(2, '2017-12-22', 100, 10, 80),
(3, '2017-12-23', 50, 100, 0);

This is what I have been trying so far, though am a Novist
trigger code

DELIMITER
$$
CREATE TRIGGER inc_trig 
BEFORE INSERT 
ON income_
FOR EaCH ROW
BEGIN
DECLARE income,expenditure,other INT;

SET income=1; SET expenditure=2; SET other=3;

IF( NEW.date_ != date_ )THEN

IF NEW.name_ = income THEN
INSERT INTO summary (date_,income)
VALUES (new.date_,new.amount);

ELSEIF NEW.name_ = expenditure THEN
INSERT INTO summary (date_,expenditure_)
VALUES (new.date_,new.amount);

ELSEIF NEW.name_ = other THEN
INSERT INTO summary (date_,other_)
VALUES (new.date_,new.amount);


ELSE(new.date_ = date_)THEN

IF NEW.name_ = income_ THEN
UPDATE summary SET income_ = income_ + new.amount);

ELSEIF NEW.name_ = expenditure THEN
UPDATE summary SET expenditure_ = expenditure_ + new.amount);

ELSEIF NEW.name_ = other THEN
UPDATE summary SET other_ = other_ + new.amount);


END IF;

END
$$

1 Answer 1

1

There's a much easier approach. First, add a unique constraint on your date column to prevent duplicate entries:

ALTER TABLE summary ADD CONSTRAINT summary_date_uniq UNIQUE(`date`);

And then use the on duplicate key update syntax when inserting new data:

INSERT INTO summary
(`date_`, `income_`, `expenditure_`, `other_`)
VALUES (2, '2017-12-22', 100, 10, 80)
ON DUPLICATE KEY UPDATE
`income` = `income_` + VALUES(`income_`),
`expenditure` = `expenditure_` + VALUES(`expenditure_`),
`others` = `others_` + VALUES(`others_`);
3
  • thanks for your reply..bit i am having this error unknown column income if i try to insert into income column and so on Commented Dec 10, 2017 at 19:31
  • @JoshuaSampson just looked at your DDL again. For some obscure reason, all your column names end with _. I've edited my answer accordingly.
    – Mureinik
    Commented Dec 10, 2017 at 19:45
  • i am trying to update his to what am doing,the reason why i made every thing short was that the code am working with is too long maybe i should post it cause this is not working Commented Dec 10, 2017 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.