Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Trigger Update with select from another table

Tags:

mysql

triggers

Just learning about triggers and I'm created the following trigger;

CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where msisdn = new.msisdn order by dat DESC limit 1);
END;

However the value does not appear to be getting updated. Any ideas?

like image 495
Michael Avatar asked May 18 '11 17:05

Michael


1 Answers

I've actually managed to solve this myself. Here is the updated code

CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON `incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where crm_record.msisdn = new.msisdn order by dat DESC limit 1);
END;

I needed to specify the table name prior to the column value on line 5.

like image 88
Michael Avatar answered Oct 28 '22 23:10

Michael