MySQL SQL UPDATE statement appears #1093 error resolution

Source: Internet
Author: User
Tags id3 mysql update

I used one of these sql:

The code is as follows Copy Code

UPDATE ' student_quiz_answer ' SET ' article_id ' = (SELECT qa.article_id, sqa.answer_id from Student_quiz_answer SQA left JOI N ' Quiz_answer ' qa on sqa.answer_id = qa.answer_id where qa.article_id > 0)

When the problem comes from the MySQL Update table field, this error message is encountered if you have a Select of its own table in the update value:

#1093 –you can ' t specify target table ' Student_quiz_answer ' for update in FROM clause

However, the SQL Update value must also be queried based on the field of the current table.


The Update value is used as a new table, such as T2, and the table alias itself is T1. The conditional clause is that the association values for T1 and T2 are equal.

To implement the method correctly, you need to create an intermediate table with the contents of the middle table exactly the same as the FRJG table, then modify the FRJG table, the middle and FRJG tables have a "primary foreign key relationship" (no such relationship exists), and modify to such an SQL statement:

The code is as follows Copy Code

UPDATE ' student_quiz_answer ' T1, (SELECT qa.article_id, sqa.answer_id from Student_quiz_answer SQA left JOIN ' Quiz_answer ' QA on sqa.answer_id = qa.answer_id where qa.article_id > 0 ' t2 SET t1. ' article_id ' = t2.article_id where t1.answer_id = t2.answer_id

Summarize

1093 error: But when you modify a table, the subquery cannot be the same table, the solution: the handle query and then set a layer, into the original table grandson query on it, for example:

Insert into  gg  set id3= (select c.a+1  from (select Max (ID3) as a ) c)    that's right. br> but insert into  gg  set id3= (select Max (ID3) as a  from GG) will report a 1093 error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.