MySQL更新一个表中的多行

时间:2018-11-01 13:58:59

标签: mysql

+-----------+---------------+-------------+---------------------+
| ProductID | Description   | CostPerItem | NumberOfItemsOnHand |
+-----------+---------------+-------------+---------------------+
| 11        | Bike          |         150 |                 200 |
| 12        | Socks         |           2 |                 100 |
| 55        | Bicycle       |         150 |                 120 |
| 66        | Elbow pads    |          14 |                 120 |
| 78        | Knee Pads     |          12 |                  70 |
| 88        | Roller Blades |          75 |                  89 |
| 99        | Helmet        |          29 |                  30 |
+-----------+---------------+-------------+---------------------+

+------------+-----------+------------+-------------+-------------+---------+
| CustomerID | FirstName | LastName   | City        | PhoneNumber | AgentID |
+------------+-----------+------------+-------------+-------------+---------+
| 10         | Barney    | Rubble     | Bedrock     |        -457 | NULL    |
| 12         | Monty     | Burns      | Springfield |         789 | NULL    |
| 13         | Wonder    | Woman      | Kypto       |        -346 | NULL    |
| 14         | Peter     | Griffens   | Providence  |       -3580 | NULL    |
| 15         | Fred      | Flintstone | Bedrock     |       -1568 | NULL    |
| 21         | Homer     | Simpson    | Springfield |       -8270 | NULL    |
| 31         | Sideshow  | Bob        | Springfield |       -4869 | NULL    |
+------------+-----------+------------+-------------+-------------+---------+

+----------+-----------+------------+
| Quantity | ProductID | CustomerID |
+----------+-----------+------------+
|       20 | 99        | 21         |
|       14 | 12        | 21         |
|       10 | 66        | 21         |
|        1 | 99        | 31         |
|        2 | 12        | 31         |
|        4 | 78        | 31         |
|        2 | 66        | 31         |
|        2 | 66        | 15         |
|        2 | 78        | 15         |
|       19 | 66        | 14         |
+----------+-----------+------------+

我有3个表,一个表包含商店产品,一个表包含客户列表,以及一个表,这些表显示购买的产品之间的关系,并由客户通过持有数量,产品ID和客户ID来表示,这两个都是它们各自表中的键< / p>

我想写一条语句,将“ Sideshow”(客户表中的名字)购买的每种产品的产品表中NumberOfItemsOnHand的值更新为20

我尝试使用以下语句:

UPDATE Product x
   SET x.NumberOfItemsOnHand = 20 
 WHERE x.ProductID = 
    (SELECT ProductID from BoughtBy WHERE BoughtBy.CustomerID = 
       (SELECT CustomerID FROM Customer WHERE FirstName = "Sideshow")
    );

我收到一条错误消息:“子查询返回多于1行”,我不知道如何做另一种方式

2 个答案:

答案 0 :(得分:1)

  • 您可以更有效地使用Inner Join来解决此问题。
  • 使用它们之间的适当关系加入所有表,并仅考虑customer.FirstName = "Sideshow"所在的行,并更新相应产品的NumberOfItemsOnHand值。

请尝试以下查询:

UPDATE Product AS p
JOIN BoughtBy AS b 
  ON b.ProductId = p.productID 
JOIN Customer AS c 
  ON c.CustomerID = b.CustomerID AND 
     c.FirstName = "Sideshow"
SET p.NumberOfItemsOnHand = 20 

答案 1 :(得分:0)

您尝试过吗:

UPDATE Product x
   SET x.NumberOfItemsOnHand = 20 
 WHERE x.ProductID IN 
    (SELECT ProductID from BoughtBy WHERE BoughtBy.CustomerID IN
       (SELECT CustomerID FROM Customer WHERE FirstName = "Sideshow")
    );