[英]SQL Update Query set Quantity
I am trying to create a query that will update my products Quantity column. 我正在尝试创建一个查询,该查询将更新我的产品“数量”列。 When a customer checks out they create a row in my orders table, from there a row is created in orderdetails for every item purchased. 当客户结帐时,他们在我的订单表中创建一行,从那里开始为所购买的每个商品创建订单明细行。
I have three tables with the following columns 我有三个带有以下各列的表
I would prefer the query look something like this, this is what I have so far. 我希望查询看起来像这样,这就是我到目前为止所拥有的。
UPDATE products SET Quantity = Quantity - orderdetails.Quantity INNER JOIN orderdetails ON products.ID = orderdetails.ProductID WHERE orderdetails.OrderID = orders.ID
OR 要么
UPDATE products SET Quantity = Quantity - orderdetails.Quantity WHERE ID IN (SELECT ProductID FROM orderdetails WHERE OrderID IN (SELECT ID FROM orders WHERE ID = 104))
MySQL error is #1054 - Unknown column 'orderdetails.Quantity' in 'field list' MySQL错误是#1054-'字段列表'中的未知列'orderdetails.Quantity'
I am still a beginner and would appreciate a nudge in the right direction, thank you. 我仍然是一个初学者,非常感谢朝着正确方向前进的人,谢谢。 If any additional information is required please let me know. 如果需要任何其他信息,请告诉我。
declare @orderID int =104 -- you can set anywhere
update products set
Quantity = Quantity - ordtl.Quantity from products p
inner join orderdetails ordtl on ordtl.ProductID = p.ID
inner join orders o on o.ID = ordtl.OrderID
where o.ID = @orderID
-- if you know order id, you can do your want -如果您知道订单ID,则可以根据需要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.