Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrong data included in before insert trigger

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think

= (select electr.id from electr)

should be

= new.id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading