Skip to main content
replaced http://dba.stackexchange.com/ with https://dba.stackexchange.com/
Source Link

#SUPPLEMENTARY INFORMATION

Specifying UPDATEs using subqueries can be dangerous business.

Back on Feb 21, 2011 someone asked this question : Problem with MySQL subqueryProblem with MySQL subquery.

I did a little research and discovered something disturbing: During the optimization of a query that involves a subquery against itself, it is entirely possible for rows to intermittently disappearDuring the optimization of a query that involves a subquery against itself, it is entirely possible for rows to intermittently disappear.

Therefore, please consider the error message

Error code: 1093
You can't specify target table 'Tab1' for update in FROM clause.

a friendly warning shot from MySQL not to write queries with that style

UPDATE Tab1  
    SET Tab1.a = '3'
    WHERE
        Tab1.id IN ( 
            SELECT Tab1.id 
            FROM Tab1, Tab2 
            WHERE Tab1.b = Tab2.b AND Tab1.c = '4'
        );

@dezso's answer@dezso's answer (gets a +1), which employs the use of an UPDATE JOIN, is more palatable to the MySQL Query Optimizer

#SUPPLEMENTARY INFORMATION

Specifying UPDATEs using subqueries can be dangerous business.

Back on Feb 21, 2011 someone asked this question : Problem with MySQL subquery.

I did a little research and discovered something disturbing: During the optimization of a query that involves a subquery against itself, it is entirely possible for rows to intermittently disappear.

Therefore, please consider the error message

Error code: 1093
You can't specify target table 'Tab1' for update in FROM clause.

a friendly warning shot from MySQL not to write queries with that style

UPDATE Tab1  
    SET Tab1.a = '3'
    WHERE
        Tab1.id IN ( 
            SELECT Tab1.id 
            FROM Tab1, Tab2 
            WHERE Tab1.b = Tab2.b AND Tab1.c = '4'
        );

@dezso's answer (gets a +1), which employs the use of an UPDATE JOIN, is more palatable to the MySQL Query Optimizer

#SUPPLEMENTARY INFORMATION

Specifying UPDATEs using subqueries can be dangerous business.

Back on Feb 21, 2011 someone asked this question : Problem with MySQL subquery.

I did a little research and discovered something disturbing: During the optimization of a query that involves a subquery against itself, it is entirely possible for rows to intermittently disappear.

Therefore, please consider the error message

Error code: 1093
You can't specify target table 'Tab1' for update in FROM clause.

a friendly warning shot from MySQL not to write queries with that style

UPDATE Tab1  
    SET Tab1.a = '3'
    WHERE
        Tab1.id IN ( 
            SELECT Tab1.id 
            FROM Tab1, Tab2 
            WHERE Tab1.b = Tab2.b AND Tab1.c = '4'
        );

@dezso's answer (gets a +1), which employs the use of an UPDATE JOIN, is more palatable to the MySQL Query Optimizer

Source Link
RolandoMySQLDBA
  • 184.3k
  • 33
  • 323
  • 531

#SUPPLEMENTARY INFORMATION

Specifying UPDATEs using subqueries can be dangerous business.

Back on Feb 21, 2011 someone asked this question : Problem with MySQL subquery.

I did a little research and discovered something disturbing: During the optimization of a query that involves a subquery against itself, it is entirely possible for rows to intermittently disappear.

Therefore, please consider the error message

Error code: 1093
You can't specify target table 'Tab1' for update in FROM clause.

a friendly warning shot from MySQL not to write queries with that style

UPDATE Tab1  
    SET Tab1.a = '3'
    WHERE
        Tab1.id IN ( 
            SELECT Tab1.id 
            FROM Tab1, Tab2 
            WHERE Tab1.b = Tab2.b AND Tab1.c = '4'
        );

@dezso's answer (gets a +1), which employs the use of an UPDATE JOIN, is more palatable to the MySQL Query Optimizer