Click here to Skip to main content
15,886,137 members
Articles / Database Development / MySQL
Tip/Trick

MySQL can’t specify target table for update in FROM clause

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
27 Oct 2014CPOL2 min read 148.4K   4   3
How to fix the error "MySQL can't specify target table for update in FROM clause" while executing an update query.
Some days back while writing an update query I faced an error “Error Code: 1093 You can’t specify target table ‘my_table’ for update in FROM clause”.
The reason for this error is that MySQL doesn’t allow updates to a table when you are also using that same table in an inner select as your update criteria.  
Other databases support this type of statement but MySQL requires a workaround.
 
Let me show you a simple example to give you the solution for this.For the example I have used Sakila database.
 
C++
UPDATE film 
SET film.language_id = 2
WHERE film.film_id IN (SELECT f.film_id 
					FROM film f
					INNER JOIN film_actor fa
					ON f.film_id = fa.film_id
					WHERE fa.actor_id = 12);
 
I know this query can be written in other way but to demonstrate the error I have written the query in this way.
 
The above query will give you the MySQL target table error because we are trying to update the film table, but 
the film table is also used to supply the list of IDs.
 
But the query will work if we write in this way
 
C++
UPDATE film 
SET film.language_id = 2
WHERE film.film_id IN (SELECT * FROM(SELECT f.film_id 
                    FROM film f
                    INNER JOIN film_actor fa
                    ON f.film_id = fa.film_id
                    WHERE fa.actor_id = 12)tblTmp);
                    
The query is basically the same, except the inner select is wrapped inside another select.
The most important thing to note is that the original select has been given an alias “tblTmp“. (The name tblTmp is arbitrary, you can give it any alias name.) 
The alias is important because assigning one will tell MySQL to create a temporary table from this select query. 
The temporary table may then be used as the source criteria for the update statement.
 
The reason it is wrapped inside another query is because MySQL syntax does not let you assign an alias to a select query when it is part of an update statement. 
So we have to put it inside another query which, I suppose separates it from the update statement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionalternative way Pin
norbertabone11-Jul-18 9:55
norbertabone11-Jul-18 9:55 
QuestionD***** limitations Pin
Jonathan Parent Lévesque5-Aug-15 8:10
Jonathan Parent Lévesque5-Aug-15 8:10 
Questionthanks for the clear explanation Pin
RobScripta12-Nov-14 0:46
professionalRobScripta12-Nov-14 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.