MySql Trigger on Insert, Update -


i have been using mysql time totally new mysql triggers.

i have database :

  • jours(jour (date, primary key), ventes (int), soldeinitial( int)) ;
  • achatscharges(id (int, primary key), libelle (varchar), prix (int), #jour (date, forein key).

after each insert or update on table ('achatscharges') trigger should check if 'soldeinitial' value in table 'jours' null update formula :

'something like( value of 'jours.soldeinitial' of last row in 'jours' - sum(achatscharges.prix) + jours.ventes'

this have tried :

create trigger updatesolde after insert, update on achatscharges each row begin if((select jours.soldeinitial jours jours.jour=new.jour) null)  update jours set jours.soldeinitial=((select jours.soldeinitial jours jours.soldeinitial not null order jours.jour desc limit 1)-sum(new.prix)+jours.ventes) jours.jour=new.jour); end if; end 

this example :

+------------+-------- +---------------------+ | jour       | ventes  | soldeinitial        |  +------------+---------+---------------------+ | 11-09-2017 | 1500    | 3000                | | 12-09-2017 | 2000    | (must calculated)| | 13-09-2017 | 4000    | (must 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 : should have 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 not explain well,this 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| trigger doesn't work here 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 clarify noted.

i've worked testdata:

create table jours     (`jour` date, `ventes` int, `soldeinitial` int) ; create table achatscharges     (`id` int, `libelle` varchar(8), `prix` int, `jour` date) ;  insert jours     (`jour`, `ventes`, `soldeinitial`) values     ('2017-09-11', 1500, 3000),     ('2017-09-12', 2000, null),     ('2017-09-13', 4000, null) ; 

i've created insert trigger this:

delimiter // drop trigger if exists insertsolde// create trigger insertsolde after insert on achatscharges each row begin declare l_previous integer; declare l_sumprix integer; if exists(select jour jours jour = new.jour)  set l_previous = (select soldeinitial jours soldeinitial not null , jour < new.jour order jour desc limit 1); set l_sumprix = (select sum(prix) achatscharges jour = new.jour group jour); update jours set soldeinitial = l_previous-l_sumprix+ventes jour=new.jour; end if; end // delimiter ; 

and tested 3 single steps

insert achatscharges     (`id`, `libelle`, `prix`, `jour`) values     (1, 'somthing', 300, '2017-09-12');  select * jours;      insert achatscharges     (`id`, `libelle`, `prix`, `jour`) values     (2, 'somthing', 200, '2017-09-12');  select * jours;      insert achatscharges     (`id`, `libelle`, `prix`, `jour`) values     (3, 'somthing', 800, '2017-09-13') ; select * jours;     

hope helps. update trigger similar.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -