1

我有一个在插入新事务时使用的存储过程。此过程正确插入到事务表中,但我还需要根据插入的值更新另一个相关表。

基于 Product_ID,我需要使用名为“Zen_Products_Description”的表中的值更新名为“Salon”的表中的 PT_Pct_to_Salon。可以使用 insert 中的“Salon_ID”找到相关的沙龙,它相当于“沙龙”表的 PK ID。

我需要插入的值在“Zen_Products_Description”表的“web_share”字段中。'Zen_Products_Description' 中的相关行可以通过将插入的值 'Product_ID' 与 'Zen_Products_Description' 的 PK 匹配,该 PK 称为 'products_id'。

我正在使用 MySQL 5。


BEGIN
INSERT INTO Transactions
(Cart_Trans_ID, Customer_ID, Pass_Through_Amt, Product_ID, Product_Name, Product_Qty, Salon_ID,  Stylist_ID, Trans_Type, customerAddress, customerCity, customerEmail, customerFirstName, customerLastName, customerPhone, customerPostal, customerState) 
VALUES (Cart_Trans_ID, Customer_ID, Pass_Through_Amt, Product_ID, Product_Name, Product_Qty, Salon_ID, Stylist_ID, Trans_Type, customerAddress, customerCity, customerEmail, customerFirstName, customerLastName, customerPhone, customerPostal, customerState);
Insert Into Zen_Products_Description
(products_id, products_name) 
Values (Product_ID, Product_Name) ON DUPLICATE KEY UPDATE
products_name = Product_Name;

//this is where I try unsuccessfully to update
update Salon
set PT_Pct_to_Salon = Zen_Products_Description.web_share
join Salon
on Salon.Salon_ID = Transactions.Salon_ID
join Zen_Products_Description
on Zen_Products_Description.products_id = Transactions.Product_ID;

END

4

1 回答 1

1

大声笑 - 你忘了问一个问题......简洁,我会帮助你

BEGIN 
  INSERT INTO Transactions 
    (Cart_Trans_ID, Customer_ID, 
     Pass_Through_Amt, Product_ID, Product_Name, 
     Product_Qty, Salon_ID, Stylist_ID, 
     Trans_Type, customerAddress, customerCity, 
     customerEmail, customerFirstName, customerLastName, 
     customerPhone, customerPostal, customerState) 
  VALUES 
    (Cart_Trans_ID, Customer_ID, 
     Pass_Through_Amt, Product_ID, Product_Name, 
     Product_Qty, Salon_ID, Stylist_ID, 
     Trans_Type, customerAddress, customerCity, 
     customerEmail, customerFirstName, customerLastName, 
     customerPhone, customerPostal, customerState); 


Insert Into Zen_Products_Description 
  (products_id, products_name) 
Values 
  (Product_ID, Product_Name) 

ON DUPLICATE KEY 

UPDATE products_name = Product_Name
########## 上面的一切都是无关紧要的
update Salon 
  set PT_Pct_to_Salon = Zen_Products_Description.web_share 
join Salon on Salon.Salon_ID = Transactions.Salon_ID 
join Zen_Products_Description 
  on Zen_Products_Description.products_id = Transactions.Product_ID;

############### here is update

UPDATE salon A 
INNER JOIN Transactions B ON A.salon_ID = B.salon_ID
INNER JOIN Zen_Products_Description C on C.Products_id = B.product_id
SET A.PT_Pct_to_Salon = C.web_share
## , ax = bx 等... ## 顺便说一下 - 学习如何格式化您的代码,以便人们可以阅读它...
END 
于 2009-01-09T01:35:00.683 回答