mysql update, insert, delete with joins
I know that you can do join statements on SELECT,
bu开发者_高级运维t can you also do joins using UPDATE, INSERT, and DELETE statements so that rows from multiple tables all get deleted/updated/inserted if they are linked with each other and they satisfy the WHERE statements...
also will this work on both LEFT JOINs, RIGHT JOINS, JOINS, and INNER JOINs?
You can do this using a trigger though
DELIMITER $$
CREATE TRIGGER au_table1_each AFTER UPDATE ON table1 FOR EACH ROW
BEGIN
UPDATE table2 SET fieldY = new.fieldX WHERE ...;
END $$
DELIMITER ;
The trigger will fire on each update and update table2 using some of the new data in table1.
There are triggers BEFORE
and AFTER
an action, which can be UPDATE
, INSERT
or DELETE
See: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
and: http://www.databasedesign-resource.com/mysql-triggers.html
You can do joins on update/insert/delete, but not to update records in those joined tables. You need to update each table individually.
精彩评论