How to Fix MySQL Error 1093 (ER_UPDATE_TABLE_USED)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 1093 when an update or delete query reads from the same table. You can fix this issue by wrapping your inner subquery inside a temporary derived table. The database engine returns the symbol ER_UPDATE_TABLE_USED and SQLSTATE HY000 with this failure. The message states that you cannot specify the target table for update in the FROM clause. This rule stops queries from changing rows while reading those exact same rows at once. When a team runs a raw cleanup script in productio

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mysql error 1093 when an update or delete query reads from the same table. You can fix this issue by wrapping your inner subquery inside a temporary derived table.

The database engine returns the symbol ER_UPDATE_TABLE_USED and SQLSTATE HY000 with this failure. The message states that you cannot specify the target table for update in the FROM clause. This rule stops queries from changing rows while reading those exact same rows at once. When a team runs a raw cleanup script in production, this block halts the job.

Why mysql error 1093 happens in the database engine

The database engine processes update and delete statements by locking the rows it needs to change. If your query also selects from that same table, the engine hits a logical conflict. Changing a row during a scan can alter the results of the inner read query. To prevent broken data and infinite loops, the parser blocks this query pattern right away.

This failure shows up during routine data maintenance and duplicate row cleanup tasks. An engineer might try to delete old user records by querying the user table for duplicates. Another common trigger is updating a summary column based on a count from the same table. Even if the inner query only reads unrelated rows, the engine still rejects the statement.

Views and common table expressions can also hide this exact conflict from the query author. If an update statement references a view that queries the target table, the parser catches it. The engine expands the view definition before checking the table locks for the active statement. When it sees the target table in both places, it stops and throws code 1093.

How to diagnose and fix the query in production

You can fix this error without turning off database safety checks or granting broad admin rights. The safest method is to rewrite the statement using a multi-table join instead of a subquery. You join the target table to itself using a clear alias for the read side. The engine handles self-joins well because it plans the read step before the write step.

Another reliable fix is wrapping the inner subquery inside a second nested subquery in the clause. This extra layer forces the engine to build a temporary derived table in memory first. Once the temporary table holds the read results, the engine releases the read lock. Then the outer update or delete statement can change the target table without any conflict.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Before running the fixed query in production, test the execution plan on a staging database. Wrapping subqueries can create large temporary tables that use up memory and slow down disk reads. Check the row counts on your staging data to make sure the join condition is accurate. Always keep role permissions tight so engineers only update the tables required for their task.

Preventing update conflicts during live database maintenance

Ad-hoc maintenance scripts often fail when engineers write hurry fixes during a live service incident. A developer might try to fix broken records or mask sensitive fields like emails or phone numbers. Running untested update queries on live tables risks locking out regular app traffic. Even when a query passes syntax checks, a bad join can wipe out critical rows.

Teams can avoid these outages by splitting the read and write steps into separate transactions. First, run a read query to fetch the primary keys of the rows you must change. Save those keys in your app logic or a dedicated scratch table for review. Next, run a simple update statement that targets only those specific primary key values.

This two-step workflow removes the subquery conflict and makes every production change easy to audit. Peer reviewers can check the exact list of target keys before any write lock happens. If the update involves customer card numbers or national IDs, masking tools can rewrite matched fields in results. Separating reads from writes keeps your database fast and prevents unexpected query parser errors.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.

With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Does mysql error 1093 happen on DELETE statements too?

Yes, the engine throws this exact same error code during delete queries that use self-referencing subqueries. The parser uses the word update in the message to mean any data change operation.

Why does adding an extra subquery layer fix ER_UPDATE_TABLE_USED?

An extra subquery forces the database to materialize the inner results into a separate temporary table. The outer query then reads from that temporary table instead of the live target table.

Can database optimizer settings trigger this error after an upgrade?

Yes, optimizer changes can merge derived tables back into the outer query to save memory. When the optimizer merges the subquery, the conflict returns and triggers code 1093.

The query runs. The emails stay hidden.