我有这个简单的更新查询:
UPDATE
erif_notify e
SET
e.current_amount = '0.00'
,e._date = now()
WHERE
id IN ( SELECT
en.id
FROM
u_contracts uc
JOIN
erif_notify en
ON
en.contract_id = uc.id
WHERE
uc._status = 7
)
但它给了我这样的错误:
You can't specify target table 'e' for update in FROM clause
为什么它会给我这个?
我知道我可以通过两个单独的查询来完成它,但可以在一个查询中完成吗?
提前谢谢。
答案 0 :(得分:1)
我认为你需要双子查询,
UPDATE
erif_notify e
SET
e.current_amount = '0.00' ,
e._date = now()
WHERE
id IN ( SELECT x.ID FROM
(
SELECT en.id
FROM u_contracts uc
JOIN erif_notify en
ON en.contract_id = uc.id
WHERE uc._status = 7
) x
)
“FROM
子句中的嵌套子查询创建了一个隐式临时表,因此它不算作您正在更新的同一个表。”
答案 1 :(得分:1)
我们走了......这个答案经过测试并且有效:
<强> SAMPLE 强>
UPDATE
erif_notify e
INNER JOIN u_contracts uc
ON e.contract_id = uc.id
AND uc._status = 7
SET
e.current_amount = '0.00',
e._date = NOW();
答案 2 :(得分:0)
像这样编辑并尝试
UPDATE
erif_notify as e
SET
e.current_amount = '0.00'
,e._date = now()
WHERE
id IN ( SELECT
en.id
FROM
erif_notify en
JOIN
u_contracts uc
ON
en.contract_id = uc.id
WHERE
uc._status = 7
)
答案 3 :(得分:0)
我几次阅读你的查询并且...
UPDATE
erif_notify e
SET
e.current_amount = '0.00'
,e._date = now()
WHERE
e.contract_id IN ( SELECT
uc.id
FROM
u_contracts uc
WHERE
uc._status = 7
)