在mysql执行下面语句时报错:
You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1
SET exam_natures_new = ''
WHERE
(outid, course_no) IN (
SELECT
a.outid,
a.course_no
FROM
edu_grade_hgm_1 a
INNER JOIN edu_grade b ON a.outid = b.outid
AND a.course_no = b.course_no
)
AND exam_natures_new IS NULL;
括号里的子查询和外面的upadate语句均没错,但加在一起便报错了。
那是因为那串英文错误提示就是说,不能先select出同一表中的某些值,
再update这个表(在同一语句中)。
所以先在子查询外面再套一层,修改sql如下:
UPDATE edu_grade_hgm_1
SET exam_natures_new = ''
WHERE
(outid, course_no) IN (
SELECT
outid,
course_no
FROM
(
SELECT
a.outid,
a.course_no
FROM
edu_grade_hgm_1 a
INNER JOIN edu_grade b ON a.outid = b.outid
AND a.course_no = b.course_no
) AS temp
)
AND exam_natures_new IS NULL;