UPDATE Products
SET TotalQuantity = (CASE WHEN P.DateOfLastUpdate < PurchaseDate
THEN (SUM(P.TotalQuantity) + SUM(M.Quantity))
ELSE P.TotalQuantity END)
FROM Products AS P ,Products_Purchased as M
WHERE P.ProductName = M.ProductName;
我想针对另一个表Products_Purchased更新表Products。如果我将新项目添加到Products_Purchased,则必须在表Products中输入,也应该更新表的TotalQuantity字段 产品。这个查询有什么问题?
我收到错误消息:
错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在产品附近使用AS P,Products_Sold作为S WHERE P.ProductName = S.ProductName&#39;在第1行
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
我已经解决了你的问题。只要按照我的所作所为......
我创建了两个表
create table products(product_name text,total_quantity int,date_of_lastupdate date);
create table products_purchased(product_name text,quantity int,date_of_purchase date);
insert into products values('food',10,'2013-5-6'),('dress',20,'2012-4-7');
insert into products_purchased('food',15,'2012-2-1'),('dress',12,'2013-6-8');
现在我尝试使用以下代码尝试更新相同的条件并且它运行良好。
只需查看代码,您就会明白我的所作所为,这将解决您的问题......
将产品更新为a,products_purchased为b set a.total_quantity =
情况下
当a.date_of_lastupdate
然后(选择* from(select sum(products.total_quantity)+ sum(products_purchased.quantity)from products natural join products_purchased)as c)
其他a.total_quantity
端
其中a.productname = b.productname;
答案 1 :(得分:0)
认为使用JOIN进行更新将有所帮助:
UPDATE
Sales_Import
SET
Sales_Import.AccountNumber = RAN.AccountNumber
FROM
Sales_Import SI
INNER JOIN
RetrieveAccountNumber RAN
ON
SI.LeadID = RAN.LeadID
答案 2 :(得分:0)
试试这个
UPDATE Products
SET TotalQuantity =
(SELECT CASE WHEN P.DateOfLastUpdate < PurchaseDate
THEN (SUM(P.TotalQuantity) + SUM(M.Quantity))
ELSE P.TotalQuantity END
FROM Products AS P ,Products_Purchased as M
WHERE P.ProductName = M.ProductName
AND P.ProductName = :p1)
WHERE ProductName = :p1
:p1是保存要更新的部件名称的参数。