Translate

Wednesday, April 29, 2015

Mysql SQL Error 1093 You can't specify target table



In this post, we will learn how to resolve this "Mysql Error 1093 You can't specify target table"



I was trying to delete some data using  this query:



DELETE  FROM articles
where art_id  in (select a.art_id
                          from articles a
                         where a.art_date <= '2015-05-19'

                        );
 

When I executed this query an error suddenly appeared that was: Mysql Error 1093 you can't specify target table.


What was the problem?
 Basically, the delete statement doesn’t allow to perform a sub-query using the same table.

How can I overcome this?

The solution, that I found was quite easy, is as simple as this: you have to wrap the
sub-query in one more select .

Solution:





DELETE FROM articles
 WHERE art_id in (
     SELECT aid FROM(
      SELECT a.art_id FROM articles a WHERE a.art_date <= '2015-05-19'
  ) as a
 );