Documentation Home
MySQL 5.6 リファレンスマニュアル
Download this Manual
PDF (US Ltr) - 26.8Mb
PDF (A4) - 26.9Mb
HTML Download (TGZ) - 7.1Mb
HTML Download (Zip) - 7.2Mb サブクエリーのエラー


  • サポートされていないサブクエリー構文:

    SQLSTATE = 42000
    Message = "This version of MySQL doesn't yet support
    'LIMIT & IN/ALL/ANY/SOME subquery'"

    これは、MySQL が次の形式のステートメントをサポートしていないことを示しています。

  • サブクエリーからの正しくないカラム数:

    SQLSTATE = 21000
    Message = "Operand should contain 1 column(s)"


    SELECT (SELECT column1, column2 FROM t2) FROM t1;


  • サブクエリーからの正しくない行数:

    SQLSTATE = 21000
    Message = "Subquery returns more than 1 row"

    このエラーは、サブクエリーが最大で 1 行しか返す必要がないにもかかわらず、複数の行を返すステートメントで発生します。次の例を考えてみます。

    SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

    SELECT column1 FROM t2 が 1 行だけを返す場合、前のクエリーは機能します。このサブクエリーが複数の行を返す場合は、エラー 1242 が発生します。その場合は、このクエリーを次のように書き換えてください。

    SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
  • サブクエリー内の誤って使用されているテーブル:

    Error 1093 (ER_UPDATE_TABLE_USED)
    SQLSTATE = HY000
    Message = "You can't specify target table 'x'
    for update in FROM clause"


    UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);

    サブクエリーは SELECT ステートメントだけでなく、UPDATE および DELETE ステートメント内でも正当であるため、UPDATE ステートメント内の割り当てのためにサブクエリーを使用できます。ただし、サブクエリーの FROM 句と更新のターゲットの両方に同じテーブル (この場合は、テーブル t1) を使用することはできません。


User Comments
  Posted by Guy Gordon on November 25, 2006
Wanting to copy a longtext field from one record to another, I first tried:
Update table set list=(select list from t1 where recno=230) where recno=169
I expected this to select one value from record 230 and copy it into record 169. Instead it fails with Error 1093. Even though this is a single scalar value, MySQL will not let you use the same table in both the update and from parts.

I got the desired result using a temp variable and two queries:
Set @Guy = (select list from t1 where recno=230);
Update t1 set list=@Guy where recno=169

Note that the semicolon separates the two statements (in phpMyAdmin). Since the temp variable is connection specific, the two queries must be run together.

  Posted by Frank Heikens on February 23, 2008
There is a workaround for the use of LIMIT in a subquery, just use a variable (seperate query, execute this one first):
SET @i = 0;

And then the select-query with the subquery including the LIMIT:
id_my_other_table IN(
SELECT id FROM my_other_table
( @i := ( @i +1 ) ) <= 10

In this case there's a limit of 10 results in the subquery.

Tested in version 5.0.45
  Posted by Cleo - on November 1, 2009
For a workaround with on the update query look at

When I try to run update query for my table "comments", MySQL returns the #1093 - You can't specify target table 'comments' for update in FROM clause message. My contrived table structure and update query are as follow:

CREATE TABLE comments(id int primary key, phrase text, uid int);

INSERT INTO comments VALUES(1, 'admin user comments',1), (2, 'HR User Comments',2), (3, 'RH User Comments',2);

UPDATE comments SET phrase =
(SELECT phrase FROM comments WHERE uid=2 AND id=2)
WHERE id = 3;

Is there any easy way to work around the #1093 - You can't specify target table 'comments' for update in FROM clause error?

Answer No: 156
Actually, your above update query seems illegal as per SQL standard. MySQL does not allow to UPDATE or DELETE a table's data if you're simultaneously reading that same data with a subquery. Because you are doing so that is why MySQL tersely said its such error message. Therefore, you will have to rewrite your above update query.

Since MySQL materializes sub queries in the FROM Clause as temporary tables, wrapping the subquery into another inner subquery in the FROM Clause causes it to be executed and stored into a temporary table, then referenced implicitly in the outer subquery. So, the update query will succeed by rewriting it like below:

UPDATE comments SET phrase =
( SELECT phrase FROM ( SELECT * FROM comments) AS c1 WHERE c1.uid=2 AND ) WHERE id =3;

  Posted by Usman Mahmood on September 30, 2010
Well, I'd a scenario where I'd a table with one BLOB field to store images. I'd about 3000 records in the database. For first, we needed a default image for all these 3000 records. So, I inserted an image into the table using phpmyadmin for the first record. Now, what I wanted to do was to copy this image from first record and update (paste) all the 2999 records instead of uploading the image for each record. I tried hard to find a solution. Asked many friends, but none of them had the answer. Eventually, I'd to create a copy of that table and then run the following query :
update table1 set image = (select image from table2 where id=1)

This worked for me ! This is the fastest and easiest way to do it. If anyone else has a better way to do it, do let me know.
Sign Up Login You must be logged in to post a comment.