DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`sale_BEFORE_DELETE` BEFORE DELETE ON `sale` FOR EACH ROW BEGIN IF OLD.quantity > 0 THEN INSERT IGNORE INTO `vn`.`routeRecalc` (`routeFk`) SELECT r.id FROM vn.sale s JOIN vn.ticket t ON t.id = s.ticketFk JOIN vn.route r ON r.id = t.routeFk WHERE r.isOk = FALSE AND s.id = OLD.id AND r.created >= util.VN_CURDATE() GROUP BY r.id; END IF; END$$ DELIMITER ;