Update with Join in MySQL
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
To update records in MySQL you often need to alter data based on information from other tables. This guide focuses on using JOIN in an UPDATE statement, a powerful technique for modifying data using a relational approach.
The basic syntax for an UPDATE statement with a JOIN is as follows:
UPDATE table1
JOIN table2 ON table1.column_name = table2.column_name
SET table1.column_to_update = new_value
WHERE condition;
table1: The table you want to update.table2: The table you’re joining with.table1.column_name, table2.column_name: Columns used to join the tables.SET: Specifies the column in table1 and the new value you want to assign.WHERE: (Optional) Specifies which rows should be updated.Let’s go through some common scenarios where UPDATE with JOIN is useful.
Suppose you have a users table and an orders table. You want to update the users table based on data in the orders table.
UPDATE users
JOIN orders ON users.user_id = orders.user_id
SET users.last_order_date = orders.order_date
WHERE orders.order_date > '2023-01-01';
Imagine a scenario where you need to increment a purchase_count in a customers table whenever a new order is placed.
UPDATE customers
JOIN orders ON customers.customer_id = orders.customer_id
SET customers.purchase_count = customers.purchase_count + 1
WHERE orders.status = 'completed';
JOIN condition are indexed. This greatly improves the performance of the query.Basedash is built as an AI-native BI platform, so teams can go from ad hoc SQL to trusted answers and dashboards quickly, without the overhead of traditional BI setup.
Using JOIN in an UPDATE statement in MySQL allows for efficient and powerful data manipulation, especially when dealing with relational data. It’s crucial to understand the syntax, employ best practices, and use the right tools to manage your databases effectively.
Written by
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.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.