简体   繁体   English

更新一个表中某个字段的所有记录,该字段的值仅在另一个表中

[英]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 |
+---------------+------------+------+--------------+
  • You can do an Inner Join between the two tables using id_product您可以使用id_product在两个表之间进行Inner Join
  • Use 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.

相关问题 如何更新其值不在另一个表中存储的范围内的记录 - How to update records whose value is not in the ranges stored in another table 如果值与另一个表中的记录值匹配,则更新表记录 - Update table records if values match records value in another table 努力在另一个表中的UPDATE值上工作,其中第一个表的所有字段均为确定值,并且第一个和其他表共享一个字段值 - Struggling to UPDATE value in another table where all field of first table are certain value and first and other table share a field value 使用另一个表中的值更新字段 - UPDATE a field with a value from another table 使用大约50000条记录更新表的所有行上的特定字段 - update a specific field on all rows of a table with about 50000 records 将所有mysql记录从一个表更新到另一个表 - Update all mysql records from one table into another 如何在此MySql表的所有记录中更改此字段的值? - How to change the value of this field in all the records of this MySql table? 如何用另一个表更新表记录 - How to update table records with another table MySQL更新字段与来自同一表中另一个字段的值 - mysql update field with value from another field in same table 从一个表中查找在另一个表的关联记录中没有特定字段值的记录的记录 - Find records from a table that, among associated records in another table, don't have a record with a specific field value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM