How to UPDATE with JOIN in MySQL (With Examples)
Learn how to update rows based on data from another table using JOIN in MySQL. Covers INNER JOIN, LEFT JOIN, subqueries, and multi-table updates.
Introduction
MySQL lets you update one table based on matching data from another table using JOIN syntax. This is essential for data synchronization, batch updates, and ETL operations.
UPDATE with INNER JOIN
Update rows in one table using values from a matching row in another table.
-- Update product prices from a price list:
UPDATE products p
INNER JOIN price_updates pu ON p.sku = pu.sku
SET p.price = pu.new_price,
p.updated_at = NOW();
Tip: MySQL UPDATE JOIN syntax puts the JOIN between UPDATE and SET — different from SQL Server.
UPDATE with LEFT JOIN
Update rows and handle cases where the joined table has no match.
-- Set discount flag based on promotions table:
UPDATE products p
LEFT JOIN promotions pr ON p.id = pr.product_id
SET p.discount = CASE
WHEN pr.id IS NOT NULL THEN pr.discount_pct
ELSE 0
END;
Tip: LEFT JOIN updates ALL rows in the target table. Use WHERE to limit scope if needed.
UPDATE with Subquery
Use a subquery when you need to update based on aggregated data.
-- Update customers with their total order count:
UPDATE customers c
INNER JOIN (
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
) o ON c.id = o.customer_id
SET c.total_orders = o.order_count;
Tip: Subqueries in UPDATE JOINs are useful for aggregations (COUNT, SUM, AVG) from related tables.
Multi-Table UPDATE
MySQL allows updating multiple tables in a single statement.
-- Update both tables at once:
UPDATE orders o
INNER JOIN customers c ON o.customer_id = c.id
SET o.status = 'archived',
c.last_order_date = o.created_at
WHERE o.created_at < '2025-01-01';
Tip: Multi-table updates are atomic — both tables update or neither does. Wrap in a transaction for safety.
Conditional UPDATE with JOIN
Apply different updates based on conditions from the joined table.
-- Tiered discount based on customer tier:
UPDATE orders o
INNER JOIN customers c ON o.customer_id = c.id
SET o.discount = CASE
WHEN c.tier = 'gold' THEN 0.20
WHEN c.tier = 'silver' THEN 0.10
ELSE 0.05
END
WHERE o.status = 'pending';
Tip: CASE WHEN inside SET gives you conditional logic. Combine with WHERE to limit which rows are updated.
Best Practices
- Always test UPDATE JOIN with a SELECT first to verify the matched rows
- Use LIMIT to test on a small batch before running on the full table
- Wrap large updates in a transaction for rollback capability
- Add a WHERE clause to limit the scope of the update
- Use EXPLAIN on the SELECT version to check performance
Generate MySQL Queries with AI2SQL
Skip the syntax lookup. Describe what you need in plain English and AI2SQL generates the correct MySQL query instantly.
No credit card required
Frequently Asked Questions
How do I update one table based on another in MySQL?
Use UPDATE table1 INNER JOIN table2 ON condition SET table1.column = table2.column. The JOIN goes between UPDATE and SET.
Can I update multiple tables in one MySQL statement?
Yes. MySQL supports multi-table UPDATE. Just add columns from both tables in the SET clause.
Can AI2SQL generate UPDATE JOIN queries?
Yes. Describe the update logic like "update product prices from the price_updates table matching on SKU" and AI2SQL generates the correct JOIN syntax.