Home > Database > Mysql Tutorial > 解决MySQL SQL语句出现#1093错误

解决MySQL SQL语句出现#1093错误

WBOY
Release: 2016-06-07 16:37:31
Original
1980 people have browsed it

今天遇到一个MySQL SQL语句问题 #1093,需要记录一下。 我使用了这样一个SQL: UPDATE `student_quiz_answer` SET `article_id`= (SELECT qa.article_id, sqa.answer_id FROM student_quiz_answer sqa LEFT JOIN `quiz_answer` qa ON sqa.answer_id = qa.ans

今天遇到一个MySQL SQL语句问题 #1093,需要记录一下。

我使用了这样一个SQL:

UPDATE `student_quiz_answer` SET `article_id`= (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)

问题来自MySQL更新Table某个字段时,如果 Update 值中存在自身Table 的 Select 就会遇到这个错误提示:

#1093 – You can’t specify target table ‘student_quiz_answer’ for update in FROM clause

但是SQL的 Update 值又必须根据当前Table的字段进行查询。

MySQL-sql-1093-error #1093 - You can't specify target table for update in FROM clause

解决方法

把 Update 值作为一个新 Table,比如 t2,本身 table 别名为 t1。条件子句就是 t1 和 t2 的关联值相等。

于是修改为这样一个 SQL 语句:

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

OK了,相当于弄个临时表,再弄个别名,再进行更新。

(...)
Read the rest of 解决MySQL SQL语句出现#1093错误 (1 words)


© lixiphp for LixiPHP, 2013. | Permalink | No comment | Add to del.icio.us
Post tags: #1093, SQL, Update

Feed enhanced by Better Feed from Ozh

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template