I use a trigger in Mysql that inserts newly inserted rows into multiple tables. The trigger is composed of several SELECTs. I noticed that the trigger works only when each SELECT returns a result. I understood that MySQL does not like to INSERT nothing... However, often, the last recorded row does not contain the data to return a result for every single SELECT of the trigger. In this case, the trigger fails to run.
For instance, when I modidy the trigger with only the SELECTs that return something, then all the INSERTs work fine.
I would like to find a way to tell the trigger to NOT execute the INSERT INTO of the SELECT that returns no result. How could I do?
delimiter |
CREATE TRIGGER trigger_name AFTER INSERT
ON table1
FOR EACH ROW
BEGIN
INSERT INTO table3 (field3a, field3b)
SELECT field1A, field1B
FROM table1
WHERE table1.id = table2.id
AND table1.id IN (SELECT max(id) FROM table1);
INSERT INTO table4 (field4a, field4b)
SELECT field2A, field2B -- imagine this SELECT returns no row
FROM table2
WHERE table2.id = table1.id
AND table1.id IN (SELECT max(id) FROM table1);
END;
|
delimiter ;
Thank you!