I am currently learning SQL and using the W3Schools Tryit Editor to play around. I am trying to update a table using a lookup from another table. I looked online and figured out the following code to run:
UPDATE OrderDetails
SET OrderDetails.ProductID = Orders.CustomerID
FROM Orders
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
But it is coming up with the following error: "Error 1: could not prepare statement (1 near ".": syntax error)"
Is there a problem with my code or are there things that W3Schools doesn't want to run?
CodePudding user response:
The update with join syntax is specific for each db
The query you are using is valid for SQLSERVER
UPDATE OrderDetails
SET OrderDetails.ProductID = Orders.CustomerID
FROM Orders
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
but not for my MySQL
UPDATE OrderDetails
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
SET OrderDetails.ProductID = Orders.CustomerID
CodePudding user response:
This works:
UPDATE OrderDetails
SET ProductID = (SELECT CustomerID FROM Orders WHERE OrderDetails.OrderID = Orders.OrderID)
WHERE OrderID IN (SELECT OrderID FROM Orders);
Is this what you want?