2016-02-13 75 views
0

我需要根據連接表中的連接和計數來更新兩個字段(Trans.PtPlanId和Trans.PtLot)。加入工作,但不是更新。根據第二表中的計數更新連接字段

該聯接返回正確的行:

SELECT 
    Plan.Prp_PartNum AS PartNum, 
    Count(Plan.Prp_PartNum) AS PartNum_Count, 
    Plan.Prp_ProdPlanId, 
    Plan.Prp_Lot, 
    Trans.PtPlanId, 
    Trans.PtLot 
FROM 
    prod_plan AS Plan 
LEFT JOIN product_trans AS Trans ON Trans.PtPartNum = Plan.Prp_PartNum 
WHERE 
    IFNULL(plan.Prp_Closed,0) = 0 AND 
    Trans.PtRole = 'XBO' AND 
    Trans.PtPosted IS NULL 
GROUP BY 
    Plan.Prp_PartNum, 
    Plan.Prp_ProdPlanId, 
    Plan.Prp_Lot 
HAVING 
    PartNum_Count = 1 
ORDER BY 
    PartNum ASC 

下面的更新產生的[錯誤] 1064:

UPDATE product_trans AS Trans 
LEFT JOIN (
    SELECT 
     Plan.Prp_PartNum AS PartNum, 
     Count(Plan.Prp_PartNum) AS PartNum_Count, 
     Plan.Prp_ProdPlanId, 
     Plan.Prp_Lot 
    FROM 
     prod_plan 
    WHERE 
     IFNULL(plan.Prp_Closed, 0) = 0 
    GROUP BY 
     Plan.Prp_PartNum, 
     Plan.Prp_ProdPlanId, 
     Plan.Prp_Lot 
    HAVING 
     Plan.PartNum_Count = 1 
    ORDER BY 
     Plan.Prp_PartNum ASC 
) AS Plan 

ON Trans.PtPartNum AS Trans = Plan.Prp_PartNum 

SET Trans.PtPlanId = Plan.Prp_ProdPlanId, 
Trans.PtLot = Plan.Prp_Lot 

WHERE 
Trans.PtRole = 'XBO' AND 
Trans.PtPosted IS NULL 

我要去哪裏錯了?

+2

你必須從'ON'條款刪除'AS Trans'。 –

+0

你應該將其添加爲答案@GiorgosBetsos –

+0

當我刪除它,我得到:[錯誤] 1054 - 未知列「Plan.Prp_PartNum」在「字段列表」 – AlphaBase

回答

0

每評論更新發布:

UPDATE product_trans AS Trans 
LEFT JOIN (
    SELECT 
     prod_plan.Prp_PartNum AS PartNum, 
     Count(prod_plan.Prp_PartNum) AS PartNum_Count, 
     prod_plan.Prp_ProdPlanId AS Plan_Id, 
     prod_plan.Prp_Lot AS Lot 
    FROM 
     prod_plan 
    WHERE 
     IFNULL(prod_plan.Prp_Closed, 0) = 0 
    GROUP BY 
     PartNum, 
     Plan_Id, 
     Lot 
    HAVING 
     PartNum_Count = 1 
    ORDER BY 
     Prp_PartNum ASC 
) AS Plan 

ON Trans.PtPartNum = Plan.PartNum 

SET Trans.PtPlanId = Plan.Plan_Id, 
Trans.PtLot = Plan.Lot 

WHERE 
Trans.PtRole = 'XBO' AND 
Trans.PtPosted IS NULL 
相關問題