Skip to content
Advertisement

Using column from another table in trigger

I need to use a column from another table when i use trigger before insert but i get an error Unknown table in field list

Here is an example code:

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

the column from another table is “printing_time_hr” from table “slicer”

Please help, what to do, how to use the column from another table

Advertisement

Answer

This works for me:

DELIMITER //
CREATE TRIGGER electricity_consumption_B_update
before insert ON electr
DECLARE _printing_time_hr    INT DEFAULT 0;
SELECT printing_time_hr INTO _printing_time_hr
  FROM slicer
 where ... ;
FOR EACH ROW 
BEGIN 
set new.electricity_consumption =  (new.`Printer_power_VT` * new.`electricity_kVT/hr` * _printing_time_hr * 0.001);
END //
DELIMITER ;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement