Home > Mobile >  Wrong data included in before insert trigger
Wrong data included in before insert trigger

Time:01-19

Im trying to create a trigger before insert that will add data in column1 table1 using data from column1 table2, but when im trying to compare id between table1 and table2 and insert, insert occurs in the next row from the desired

Here is my trigger code:

DELIMITER //
CREATE TRIGGER electricity_consumption_update
before insert ON electr
FOR EACH ROW 
BEGIN 
set new.electricity_consumption =  
(new.`Printer_power_VT` * new.`electricity_kVT/hr` * 
(select printing_time_hr from slicer where slicer.id = (select electr.id from electr)) * 0.001);
END //
DELIMITER ;

Here im trying to compare id of table "slicer" and "electr" before insert, but when i insert the first row, the column i want to fill by this trigger is empty, and when i insert the second row, the column i fill with the trigger is not empty but gets the data that had to be in a previous row

CodePudding user response:

I think

= (select electr.id from electr)

should be

= new.id
  •  Tags:  
  • Related