[英]Update all the records of a field in a table whose value is only in another table
I have table A:我有表A:
+---------------+------------+-------+-------------+
| name | id_product | price | price_medium|
+---------------+------------+-------+-------------+
| phone | 1 | 300 | 300 |
| mouse | 2 | 50 | 75 |
| phone | 1 | 250 | 300 |
| keyboard | 3 | 100 | 100 |
| mouse | 2 | 100 | 75 |
| phone | 1 | 350 | 300 |
+---------------+------------+------+--------------+
In table B is a temporary table, products are added each time a purchase is made and then deleted.表B是一个临时表,每次购买时添加产品然后删除。 When it is sent, I want to update in table A the column "price_medium" that I have in table B whose product_id is the addition.发送时,我想在表 A 中更新表 B 中的列“price_medium”,其 product_id 是添加项。
+---------------+------------+-------+-------------+
| name | id_product | price | price_medium|
+---------------+------------+-------+-------------+
| phone | 1 | 100 | 220 |
| mouse | 2 | 125 | 92 |
| phone | 1 | 100 | 220 |
+---------------+------------+------+--------------+
The final result would be in table A最终结果将在表 A 中
+---------------+------------+-------+-------------+
| name | id_product | price | price_medium|
+---------------+------------+-------+-------------+
| phone | 1 | 300 | 220 |
| mouse | 2 | 50 | 92 |
| phone | 1 | 250 | 220 |
| keyboard | 3 | 100 | 100 |
| mouse | 2 | 100 | 92 |
| phone | 1 | 350 | 220 |
+---------------+------------+------+--------------+
Inner Join
between the two tables using id_product
您可以使用id_product
在两个表之间进行Inner Join
Set
to update price_medium
in tableA (equal to price_medium
in tableB)使用Set
更新 tableA 中的price_medium
(等于 tableB 中的price_medium
)Try:尝试:
UPDATE tableA AS tA
JOIN tableB AS tB ON tB.id_product = tA.id_product
SET tA.price_medium = tB.price_medium
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.