开发者

mysql update, insert, delete with joins

开发者 https://www.devze.com 2023-03-06 07:05 出处:网络
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/inser

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号