MySQL Join Update Syntax
This article provides insights into MySQL's method of executing join updates, which involve updating table columns based on data retrieved from joined tables.
Using Join Update Syntax
Let's consider the scenario where you have two tables:
Objective: Increment the train's capacity when a reservation is canceled.
Syntax:
UPDATE Reservations r JOIN Train t ON (r.Train = t.TrainID) SET t.Capacity = t.Capacity + r.NoSeats WHERE r.ReservationID = ?;
Here's how it works:
Incrementing by Arbitrary Seats
To increment by an arbitrary number of seats, simply modify the SET clause as follows:
SET t.Capacity = t.Capacity + ?
Transaction Considerations
The above is the detailed content of How to Use MySQL JOIN for Updating Table Columns Based on Joined Table Data?. For more information, please follow other related articles on the PHP Chinese website!