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
- orders (ID, Quantity)
- orderdetails (ID, OrderID, ProductID, Quantity) "OrderID and ProductID are foreign keys"
- products (ID)
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'
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.