DROP TRIGGER /*!50032 IF EXISTS */ `sales_update`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `sales_update` AFTER UPDATE ON `sales`
FOR EACH ROW BEGIN
IF NOT OLD.id_parent <=> NEW.id_parent THEN
/*find all records for which the vertices of underlying referenced above, facing the top and remove them */
DELETE sales_tree_p t1 FROM sales_tree_p t1
JOIN (SELECT * FROM sales_tree_p WHERE id_sale_in = OLD.id_sale) t2 ON t2.id_sale = t1.id_sale
JOIN (SELECT * FROM sales_tree_p WHERE id_sale = OLD.id_parent) t3 ON (t3.id_sale_in = t1.id_sale_in);
/* calculate new values for the pair of underlying vertices and add them to the table */
INSERT INTO sales_tree_p (id_sale, id_sale_in)
SELECT t1.id_sale, t2.id_sale_in FROM sales_tree_p t1
JOIN (SELECT * FROM sales_tree_p WHERE id_sale = NEW.id_parent) t2
WHERE t1.id_sale_in = OLD.id_sale;
/* maybe used for OQGRAPH
update mega_test set origid = NEW.id_sale, destid = NEW.id_parent
WHERE origid = OLD.id_sale AND destid = OLD.id_parent; */
END IF;
END;
$$
Trigger example:
DELIMITER $$
USE `banktest`$$
DROP TRIGGER /*!50032 IF EXISTS */ `sales_update`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `sales_update` AFTER UPDATE ON `sales`
FOR EACH ROW BEGIN
IF NOT OLD.id_parent <=> NEW.id_parent THEN
/*find all records for which the vertices of underlying referenced above, facing the top and remove them */
DELETE sales_tree_p t1 FROM sales_tree_p t1
JOIN (SELECT * FROM sales_tree_p WHERE id_sale_in = OLD.id_sale) t2 ON t2.id_sale = t1.id_sale
JOIN (SELECT * FROM sales_tree_p WHERE id_sale = OLD.id_parent) t3 ON (t3.id_sale_in = t1.id_sale_in);
/* calculate new values for the pair of underlying vertices and add them to the table */
INSERT INTO sales_tree_p (id_sale, id_sale_in)
SELECT t1.id_sale, t2.id_sale_in FROM sales_tree_p t1
JOIN (SELECT * FROM sales_tree_p WHERE id_sale = NEW.id_parent) t2
WHERE t1.id_sale_in = OLD.id_sale;
/* maybe used for OQGRAPH
update mega_test set origid = NEW.id_sale, destid = NEW.id_parent
WHERE origid = OLD.id_sale AND destid = OLD.id_parent; */
END IF;
END;
$$
DELIMITER ;