MySQL Tutorials - Herong's Tutorial Examples - v4.46, by Herong Yang
UPDATE with Joined Tables
This section provides a tutorial example on how to use UPDATE statements to update records in one or multiple tables with joined tables.
In MySQL, you can update columns in a table joined with other tables using the following syntax.
UPDATE tbl_1, tbl_2 SET tbl_1.col=exp, tbl_1.col=exp, ... WHERE ...
For example, the following statement updates Document's Language with Author's language:
update Document d, Author a set d.Language = a.Language where d.AuthorID = a.ID and d.Language is null and a.Language is not null;
You can update columns in multiple joined tables using the following syntax.
UPDATE tbl_1, tbl_2 SET tbl_1.col=exp, tbl_2.col=exp, ... WHERE ...
Here is perfect example from Irfan published on stackoverflow.com:
UPDATE Books, Orders SET Orders.Quantity = Orders.Quantity + 2, Books.InStock = Books.InStock - 2 WHERE Books.BookID = Orders.BookID AND Orders.OrderID = 1002;
Table of Contents
MySQL Introduction and Installation
Introduction of MySQL Programs
Perl Programs and MySQL Servers
Java Programs and MySQL Servers
Character Strings and Bit Strings
Table Column Types for Different Types of Values
Using DDL to Create Tables and Indexes
►Using DML to Insert, Update and Delete Records
INSERT INTO - Statement to Insert Records to Tables
UPDATE - Statement to Update Records in Tables
DELETE FROM - Statement to Delete Records from Tables
Using SELECT to Query Database
Window Functions for Statistical Analysis
Use Index for Better Performance
Transaction Management and Isolation Levels
Defining and Calling Stored Procedures
Variables, Loops and Cursors Used in Stored Procedures
System, User-Defined and Stored Procedure Variables
Storage Engines in MySQL Server
InnoDB Storage Engine - Primary and Secondary Indexes
Performance Tuning and Optimization
Installing MySQL Server on Linux