0

我有这个简单的更新查询:

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

为什么给我这个?

我知道我可以通过两个单独的查询来完成,但是可以一次完成吗?

先感谢您。

4

4 回答 4

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子句中的嵌套子查询创建了一个隐式临时表,因此它不会算作您正在更新的同一个表。”

于 2012-09-25T12:52:54.483 回答
1

在这里我们开始......这个答案已经过测试并且有效:

样本

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();
于 2012-09-25T12:53:24.263 回答
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
      )
于 2012-09-25T12:50:45.967 回答
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
          )
于 2012-09-25T13:08:01.463 回答