Home > Database > Mysql Tutorial > How to Solve MySQL Error 1093: Can't Specify Target Table in FROM Clause?

How to Solve MySQL Error 1093: Can't Specify Target Table in FROM Clause?

Susan Sarandon
Release: 2024-12-22 07:29:09
Original
584 people have browsed it

How to Solve MySQL Error 1093: Can't Specify Target Table in FROM Clause?

MySQL Error 1093: Can't Specify Target Table in FROM Clause

When attempting to execute an update query on a table referenced in the FROM clause, you may encounter MySQL error 1093. This is because MySQL forbids modifications to tables directly involved in the selection process.

One alternative is to join the table to itself. By creating an alias for the table, you can select from it multiple times and modify the original table without the error:

UPDATE tbl AS a
INNER JOIN tbl AS b ON a.id = b.id
SET a.col = b.col
Copy after login

Another solution is to nest the subquery deeper into the FROM clause:

UPDATE tbl SET col = (
  SELECT ... FROM (SELECT.... FROM) AS x);
Copy after login

This workaround creates an implicit temporary table from the subquery, allowing for an update without the error. However, this method may have performance implications.

It's important to note that in MySQL 5.7.6 and later, the optimizer may optimize out the subquery, resulting in the same error. If you encounter this, consider temporarily disabling the optimization:

SET optimizer_switch = 'derived_merge=off';
Copy after login

The above is the detailed content of How to Solve MySQL Error 1093: Can't Specify Target Table in FROM Clause?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template