MySQL: “Unknown column in where clause” during Upd

2019-07-09 09:42发布

问题:

UPDATE Recipes RE, (
    SELECT SUM((((i.iCaseCost/i.iCaseQty)/i.iUnitSize)/i.iUnitSoldBy)*ri.riQty*ri.riMeasureBy) AS     'RecipeCost'
      FROM Recipes r INNER JOIN RecipeIngredients ri
        ON r.rID = ri.rID JOIN Ingredients i
        ON ri.iID = i.iID 
        WHERE ri.rID = RE.rID
    ) t
    SET RE.rYieldCost = t.RecipeCost

When executed, I get the following error: "Unknown column 'RE.rID' in 'where clause'".

Any ideas?

回答1:

Your inner derived query doesn't know anything about columns in the outer query. Try moving the WHERE into the UPDATE clause with something like WHERE t.rID = RE.rID.