如何更新一个表以设置从另一个表中选择的位置?

时间:2019-09-11 20:26:54

标签: mysql

我必须用levelfour的Cost更新component_phase,但我还必须加入plancomponent以获得密钥。

我认为我很接近答案,但这是不对的。

component_phase
*idPlanComponent
costEstimate

plancomponent
*idPlanComponent
*LevelFourId
*idPlan


levelfour
*LevelFourId
Cost


UPDATE component_phase 
SET costEstimate = (SELECT Cost FROM levelfour, plancomponent 
    WHERE levelfour.LevelFourId = plancomponent.LevelFourId 
    AND idPlan = :idPlan) 
WHERE idPlanComponent in (SELECT idPlanComponent 
    FROM plancomponent 
    WHERE idPlan = :idPlan

1 个答案:

答案 0 :(得分:2)

您可以使用MySQL UPDATE JOIN进行更新。

UPDATE component_phase cp
INNER JOIN plancomponent p ON cp.idPlanComponent = p.idPlanComponent
INNER JOIN levelfour l ON l.LevelFourId = p.LevelFourId
SET cp.costEstimate = l.Cost
WHERE p.idPlan = :idPlan;