I have a trigger in MySQL table. Table has about 20 columns.
DELIMITER $$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `completion_date_update`
BEFORE UPDATE ON `orders`
FOR EACH ROW
BEGIN
IF NEW.`order_status` = 'COMPLETED' THEN
SET NEW.`completion_date` = NOW();
END IF;
IF NEW.`order_status` != 'COMPLETED' THEN
SET NEW.`completion_date` = NULL;
END IF;
END$$
What I want is this trigger to update completion_date column only when column order_status is updated. If any other column is updated this trigger should not do anything.
What is actually happening is this trigger is updating completion_date upon any column update.
Can anyone explain to me why does this happen and what did I do wrong?
order_status column is NOT NULL and has possible values as below:
enum('NEW','OPEN','COMPLETED','CANCELLED','REPLACED')
Default is 'NEW'
Thanks
you should also compare if
order_statushas been changed and that the new value isCOMPLETEDand you can remove the other
IFbelow.