我想更新表格 cases_proceedings ,但会在其他表 cases_attach_files 中检查一些条件,即
SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND
cp.`is_decided` = 0
但是,当我想要更新第一个表值并使用 IN 操作搜索第一个表的 sno 时,使用子查询,如下所示:
UPDATE cases_proceedings
SET `is_decided` = 1
WHERE sno IN(SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND
cp.`is_decided` = 0);
显示错误:
Error Code: 1093
You can't specify target table 'cases_proceedings' for update in FROM clause
我不知道如何解决这个问题?
答案 0 :(得分:0)
这里的一个解决方法是将子查询包装在另一个子查询中:
UPDATE cases_proceedings
SET is_decided = 1
WHERE sno IN
(
SELECT sno
FROM
(
SELECT cp.sno
FROM cases_proceedings cp
INNER JOIN cases_attach_files cf
ON cp.case_sno = cf.case_sno AND
cf.page_lbl_sno = 10 AND
cp.is_decided = 0
) t
);
额外的子查询可以解决错误,因为它强制MySQL实现包含连接的子查询。
答案 1 :(得分:0)
根据documentation,在MySQL中,您无法修改在SELECT部分中使用的同一个表。
您可以将其包装在子查询中或连接两个表,如下所示:
UPDATE cases_proceedings cp
INNER JOIN cases_attach_files cf
ON cp.case_sno = cf.case_sno
AND cf.page_lbl_sno = 10
AND cp.is_decided = 0
SET cp.is_decided = 1