Skip to content

Joining tables in an UPDATE statement is a powerful way to modify data in one table based on data in another table. This guide will walk you through the intricacies of using JOIN with the UPDATE statement in MySQL.

1. Basic Syntax

Here’s the basic syntax for using JOIN in an UPDATE statement:

UPDATE table1
JOIN table2
ON table1.column1 = table2.column2
SET table1.target_column = value
WHERE some_condition;

2. Simple Example

Imagine you have two tables:

  1. users: with columns user_id and email
  2. email_preferences: with columns email and send_promotions

You want to set a user’s email in the users table to ‘[email protected]’ for those who have opted out of promotions.

UPDATE users
JOIN email_preferences
ON users.email = email_preferences.email
SET users.email = '[email protected]'
WHERE email_preferences.send_promotions = 0;

3. Using Multiple Tables

You can also join multiple tables in an UPDATE statement.

Let’s say you have an additional table:

  1. user_profiles: with columns user_id and profile_status

You want to set the profile status to ‘inactive’ for users who have opted out of promotions:

UPDATE user_profiles
JOIN users ON user_profiles.user_id = users.user_id
JOIN email_preferences ON users.email = email_preferences.email
SET user_profiles.profile_status = 'inactive'
WHERE email_preferences.send_promotions = 0;

4. Using LEFT JOIN

Sometimes you might want to update records in one table based on the absence of records in another table. This is where the LEFT JOIN comes in handy.

For example, if you want to set the email to ‘[email protected]’ for users who don’t have any email preferences:

UPDATE users
LEFT JOIN email_preferences
ON users.email = email_preferences.email
SET users.email = '[email protected]'
WHERE email_preferences.email IS NULL;

5. Tips

  1. Backup First: Always backup your database before running UPDATE queries, especially in production. An erroneous query can modify a lot of rows unintentionally.
  2. Use Transactions: Use transactions to make sure that your UPDATE statements can be rolled back in case of errors.
  3. Test First: Before executing the UPDATE, you can replace UPDATE with SELECT to preview the rows that would be affected.
-- Instead of:
-- UPDATE table1 JOIN table2 ...

-- Use:
SELECT * FROM table1
JOIN table2 ...

Conclusion

The ability to join tables in an UPDATE statement is a powerful tool in MySQL. With this guide, you now understand the basics of using JOIN with UPDATE and can modify data across multiple related tables effectively. Always remember to proceed with caution when modifying data, and happy querying!

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.