Mysql 更新多行表,只有子查询和第一次匹配

时间:2021-02-15 14:51:47

标签: mysql

示例表:

| sku          | sold_for | shipping_and_handling | order_number|   
| A229AR       | 40.00    |     8.00              | 27-06341    |
| R255         | 28.12    |     13.00             | 24-06370    |
|              | 58.58    |     16.00             | 22-06449    |
| A249AR-R     | 29.78    |     0.00              | 22-06449    |
| A278AR       | 28.80    |     0.00              | 22-06449    |
| S19-1.       | 33.57    |     0.00              | 22-06449    |
|              | 20.00    |     9.00              | 24-06234    |
| R256         | 40.08    |     8.00              | 05-06375    |
| B232         | 40.08    |     0.00              | 05-06375    |
| C201         | 40.08    |     0.00              | 05-06375    |
UPDATE table1 SET shipping_and_handling = (SELECT shipping_and_handling FROM table1
WHERE sku = '' AND order_number = '22-06449')
WHERE sku != '' AND order_number = '22-06449' LIMIT 1

我需要更新shipping_and_handling,其中order_number 超过一个,并更新相同order_number 的唯一第一条记录。我知道如何更新一行,但不知道如何更新所有记录。 问题:如何更新所有记录?

结果

| sku          | sold_for | shipping_and_handling | order_number|   
| A229AR       | 40.00    |     8.00              | 27-06341    |
| R255         | 28.12    |     13.00             | 24-06370    |
|              | 58.58    |     16.00             | 22-06449    |
| A249AR-R     | 29.78    |     **16.00**         | 22-06449    |
| A278AR       | 28.80    |     0.00              | 22-06449    |
| S19-1.       | 33.57    |     0.00              | 22-06449    |
|              | 20.00    |     9.00              | 24-06234    |
| R256         | 40.08    |     8.00              | 05-06375    |
| B232         | 40.08    |     **8.00**          | 05-06375    |
| C201         | 40.08    |     0.00              | 05-06375    |

0 个答案:

没有答案