[SOLVED] MYSQL TRIGGER (BEFORE INSERT) gives me NULL

Issue

please I have a probleme with MYSQL TRIGGER :

CREATE TRIGGER BEFORE INSERT ON `table_A` 
FOR EACH ROW
SET NEW.name = (SELECT `table_B`.`name` FROM `table_A` INNER JOIN `table_B` 
ON `table_A`.`table_B_id` = `table_B`.`id` WHERE `table_A`.`id` = NEW.id);

So I need that col name in table_A fill from table_B using the NEW.id in join.
it’s like the NEW.id is NULL so that col fill with NULL
any ideas
and thank you in advance

Solution

There seems to be no reason to include table_A in the select or WHERE table_A.id = NEW.id)

SET NEW.name = (SELECT `table_B`.`name` FROM `table_B` 
ON new.`table_B_id` = `table_B`.`id` );

If this doesn’t work for you add sample data and expected output as text to the question

Answered By – P.Salmon

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.