Updating Multiple Tables with Similar Values in MySQL
In the scenario where you have two tables requiring the same updates for denormalization, you can utilize multi-table updates offered by MySQL. This approach allows for simultaneous updates across multiple tables with a single statement.
To achieve this:
For instance, consider the following query:
UPDATE Table_One a INNER JOIN Table_Two b ON (a.userid = b.userid) SET a.win = a.win+1, a.streak = a.streak+1, a.score = a.score+200, b.win = b.win+1, b.streak = b.streak+1, b.score = b.score+200 WHERE a.userid = 1 AND a.lid = 1 AND b.userid = 1
Here, both tables' win, streak, and score columns are updated incrementally for a specific userid and lid value. Note that the lid column is not present in Table_Two, so its value is not updated.
Additional Considerations:
The above is the detailed content of How to Efficiently Update Multiple Tables with Similar Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!