无法更新以更新表,因为它在子查询的from子句中使用

时间:2019-03-28 10:05:13

标签: mysql sql

UPDATE qn_answers
SET `GRADE` = -100
WHERE `QSN_ID` IN (SELECT q.`ID` FROM `questions` q
LEFT JOIN qn_answers qa ON q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0);

我正在尝试根据联接表中的结果更新列,但它给出错误:1093

3 个答案:

答案 0 :(得分:1)

我想您想在这里加入更新,而没有子查询:

UPDATE qn_answers qa
INNER JOIN questions q
    ON qa.QSN_ID = q.ID
SET GRADE = -100
WHERE
    q.QSN_TYPE_ID = 3 AND
    qa.ISCORRECT = 0  AND
    qa.GRADE = 0;

该错误消息与您将目标表qn_answers用作联接的一部分有关。您不能直接在MySQL更新中执行此操作。

答案 1 :(得分:0)

使用join update

UPDATE qn_answers a
join 
(SELECT q.`ID` FROM `questions` q
LEFT JOIN qn_answers qa ON q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0
) b on a.`QSN_ID`=b.`ID`
SET `GRADE` = -100

答案 2 :(得分:0)

我认为标准更新也可以在这里使用。试试这个:

UPDATE qa
SET `GRADE` = -100
FROM 
`questions` q
LEFT JOIN
`qn_answers` qa
ON
q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0;