Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing string in mysql trigger

Here is a trigger i created and i want to compare new.utype with employee as given below but dont know the actual way to compare it in mysql .. how can i achieve this and also please tell if condition syntax is correct or not

delimiter $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF(new.utype =='employee') THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE 
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;
like image 438
Ayush Jain Avatar asked Apr 22 '26 04:04

Ayush Jain


1 Answers

Use "like" key word as below

IF new.utype like 'employee' 

Entire trigger looks like this

delimiter $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype like 'employee'  THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE 
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;

I assumed trigger you wrote other than the string comparison is correct Thanks

like image 52
Chinthaka Suren Fernando Avatar answered Apr 24 '26 08:04

Chinthaka Suren Fernando



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!