假设我有 2 个表:
第一个是产品:
+------------------+-----------------+--------+
| Product Name | Type | Price |
+------------------+-----------------+--------+
| Carrots | Vegetables | 10 |
| Oranges | Fruits | 20 |
| Beer | Drinks | 50 |
| Milk | Dairy products | 30 |
| Burgers | meat | 40 |
| Baguettes | bread | 10 |
第二个是组合:
+-----------+---------+-----------+-------------+
| Item1 | Item2 | Item3 | TotalPrice |
+-----------+---------+-----------+-------------+
| Carrots | Oranges | Beer | |
| Milk | Burgers | Baguettes | |
+-----------+---------+-----------+-------------+
我想根据其产品的总价更新每个组合的总价。
例如,牛奶、汉堡和长棍面包组合的“总价”列将为 80。
我怎样才能做到这一点?
最佳答案
您可以使用子查询根据组合的项目计算总和:
UPDATE combos SET total_price = (SELECT SUM(price) FROM products
WHERE product_name IN (item1, item2, item3));
关于mysql - 根据 MYSQL 中的另一个表值更新表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347049/