T
T
Tonako2015-10-06 09:34:26
MySQL
Tonako, 2015-10-06 09:34:26

How to create a trigger?

Help to create a trigger
Task - there are requests like: (reserve always increases, id is different)

UPDATE `currency` SET `reserve` = '100' WHERE `id` = 5;

You need to make a trigger that does another update if id = 11
and set the reserve delta from the previous request for a new update, I imagine it's done like this:
UPDATE `currency` SET `reserve` = `reserve`+(NEW.reserve-OLD.reserve) WHERE `id` = 9;

In other words: if the reserve of id 11 is updated, then you need to update the reserve of id 9 by adding reserve id 9 and calculating the delta of reserve id 11

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-10-06
@MaxDukov

DELIMETER //
CREATE TRIGGER trg_upd_reserve AFTER UPDATE ON currency 
BEGIN
  IF NEW.id=11
  UPDATE 'currency' SET `reserve` = `reserve`+(NEW.reserve-OLD.reserve) WHERE `id` = 9;
  END IF;
END;//
DELIMETER ;

something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question