Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 37.9Mb
PDF (A4) - 38.0Mb
PDF (RPM) - 36.5Mb
HTML Download (TGZ) - 9.9Mb
HTML Download (Zip) - 9.9Mb
HTML Download (RPM) - 8.7Mb
Man Pages (TGZ) - 210.0Kb
Man Pages (Zip) - 319.1Kb
Info (Gzip) - 3.5Mb
Info (Zip) - 3.5Mb
Excerpts from this Manual Subquery Errors

There are some errors that apply only to subqueries. This section describes them.

  • Unsupported subquery syntax:

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

    This means that MySQL does not support statements of the following form:

  • Incorrect number of columns from subquery:

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

    This error occurs in cases like this:

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

    You may use a subquery that returns multiple columns, if the purpose is row comparison. In other contexts, the subquery must be a scalar operand. See Section, “Row Subqueries”.

  • Incorrect number of rows from subquery:

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

    This error occurs for statements where the subquery must return at most one row but returns multiple rows. Consider the following example:

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

    If SELECT column1 FROM t2 returns just one row, the previous query will work. If the subquery returns more than one row, error 1242 will occur. In that case, the query should be rewritten as:

    SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
  • Incorrectly used table in subquery:

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

    This error occurs in cases such as the following, which attempts to modify a table and select from the same table in the subquery:

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

    You can use a subquery for assignment within an UPDATE statement because subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table (in this case, table t1) for both the subquery FROM clause and the update target.

For transactional storage engines, the failure of a subquery causes the entire statement to fail. For nontransactional storage engines, data modifications made before the error was encountered are preserved.

User Comments
User comments in this section are, as the name implies, provided by MySQL users. The MySQL documentation team is not responsible for, nor do they endorse, any of the information provided here.
  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.