更新两个表 - 错误 - 未知列" product_varities.product_varities_id'在' where子句'

时间:2015-04-25 18:52:28

标签: mysql stored-procedures

我正在尝试更新两个表,从一个表中获取数据并更新第二个表。但是我一直收到错误:

  

1054 - MySQL中的未知列异常。

Orderlines
orderlinesid
quantity
product_varities_id (fk)

Product Varities
product_varities_id (pk)
quantity

存储过程

BEGIN
DECLARE quantityToAddBack int(5);

select orderlines.quantity from orderlines where orderlines.order_id = iorderid AND product_varities.product_varities_id = iproductvaritiesid into quantityToAddBack;
-- do update FIRST
UPDATE orderlines set orderlines.`status_id` = 2
WHERE orderlines.order_id = iorderid AND orderlines.product_varities_id = iproductvaritiesid;

-- then update quantity
update product_varities set product_varities.quantity = product_varities.quantity + quantityToAddBack
WHERE product_varities.product_varities_id = iproductvaritiesid;

END

我不明白我做错了什么,我正在执行两个陈述,因为我在这里的研究发现你不能一次更新两个表。

1 个答案:

答案 0 :(得分:0)

从给定的描述看,iorderidiproductvaritiesid是过程的输入值。

因此,过程中的查询应该是

select 
ol.quantity into quantityToAddBack
from orderlines ol
join product_varities pv on pv.product_varities_id = ol.product_varities_id
where ol.order_id = iorderid ;

-- do update FIRST
UPDATE orderlines set `status_id` = 2
WHERE order_id = iorderid 
AND product_varities_id = iproductvaritiesid;


-- then update quantity
update 
product_varities 
set quantity = quantity + quantityToAddBack
WHERE product_varities_id = iproductvaritiesid;