在where子句但列存在的MySQL未知列

时间:2013-01-31 04:47:58

标签: mysql sql join sql-update

所以看起来有些人以前遇到过这个问题,但搜索没有找到我的答案。

我正在运行

 UPDATE ProdCosts SET PercentCost = ((ProdCosts.PieceCost/totalprodcost.sumpiececost)*100)
 WHERE totalprodcost.ProdNo=ProdCosts.ProdNo;
 AND ProdCosts.PieceCost > 0;

并收到错误

MySQL said: Documentation
#1054 - Unknown column 'totalprodcost.ProdNo' in 'where clause' 

DESCRIBE totalprodcost显示:

Field   Type    Null    Key     Default     Extra
ProdNo  int(11) YES         NULL    
sumpiececost    double  YES         NULL    

我无法弄清楚这一点。看起来很简单。是什么给了什么?

2 个答案:

答案 0 :(得分:0)

加入UPDATE语句

上的表格
UPDATE  ProdCosts a
        INNER JOIN totalprodcost b
            ON b.ProdNo = a.ProdNo
SET     a.PercentCost = ((a.PieceCost/b.sumpiececost)*100)
WHERE   a.PieceCost > 0;

答案 1 :(得分:0)

试试此查询

UPDATE ProdCosts, totalprodcost SET ProdCosts.PercentCost = ((ProdCosts.PieceCost/totalprodcost.sumpiececost)*100) WHERE totalprodcost.ProdNo=ProdCosts.ProdNo AND ProdCosts.PieceCost > 0;