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

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 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

UPDATE with Joined 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

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB