不能在 FROM 子句中指定要更新的目标表 XXX(子句中带有 SUM)



我正在尝试执行这个查询:

UPDATE  
arc_salon_credit_exposant AS asce
SET  
asce.asce_credit_restant = 
(
SELECT 
SUM(asce_credit_restant) 
FROM
arc_salon_credit_exposant
WHERE
asce_scre_id = '524' AND
asce_sexp_id = '719' AND
(asce_fam_id is NULL OR
asce_fam_id = '168')
)
WHERE  
asce.asce_scre_id = '524' AND
asce.asce_sexp_id = '719' AND
asce.asce_fam_id is NULL

但是,我得到的只是一个mysql错误(#1093-您不能在FROM子句中指定更新的目标表"asce")。我在stackoverflow上读到了一些问题(这就是我尝试使用别名的原因),但我无法使用它。我知道我必须编写查询,所以Mysql将创建一个临时表,但是。。我做不到。有点被困在这里了。

这是表格的结构:

Column name Type    Null    Défaut
asce_id int(11) Non 
asce_scre_id    int(11) Non 
asce_sexp_id    int(11) Non 
asce_credit_restant double  Oui NULL
asce_fam_id int(11) Oui NULL

这里有一些数据:

asce_id asce_scre_id asce_sexp_id asce_credit_restant asce_fam_id

35 524 7885 4900空

17 524 719 200空

45 524 719 100 168

44 524 7885 100 168

提前感谢

您需要使用类似的联接来更新您的表

UPDATE  arc_salon_credit_exposant AS asce
JOIN
(     SELECT 
asce_credit_restant.IDColumn  --<<here your Unique column for self join
SUM(asce_credit_restant) as tot
FROM
arc_salon_credit_exposant
WHERE
asce_scre_id = '524' AND
asce_sexp_id = '719' AND
asce_fam_id is NULL
) A
ON asce.IDColumn = A.IDColumn
SET asce.asce_credit_restant = A.tot
WHERE  
asce.asce_scre_id = '524' AND
asce.asce_sexp_id = '719' AND
asce.asce_fam_id is NULL

请参阅此示例演示

最新更新