duanchui1279 2013-05-09 08:00
浏览 34
已采纳

我是否需要在mysql或其他使用PHP的方法中使用总和?

Ok I have a query I need to run in php on my mysql database and just wondering the best way to do it...

I need to add together all the quantities of the product options and use that to get a total for the product quantity.


Table product.

In the product table each product has a 'product_id'. Each product contains a column 'quantity' which in most cases is inaccurate.

+-----------+------------+
| ProductID |  Quantity  |
+-----------+------------+
|         1 | 3          |
|         2 | 5          |
|         3 | 1          | 
|         4 | 1          | 
|         5 | 4          |
|         6 | 8          |
|         7 | 2          |
+-----------+------------+

Table product_option_value.

In the product_option_value table each option uses the 'product_id' and contains a column 'quantity' for each option.

+-------------------------+------------+---------------+
| product_option_value_id | product_id |    quanity    |
+-------------------------+------------+---------------+
|            200          |      1     |      12       |
|            200          |      1     |      11       |
|            200          |      1     |      44       |
|            200          |      1     |       4       |
|            204          |      3     |       3       |
|            204          |      3     |       7       |
|            204          |      3     |       9       |
+-------------------------+------------+---------------+

I need to update the 'product' table 'quantity' column with the correct quantities from the 'product_option_value' table with the sum of all the 'quantity' columns where the product_id is the same as the product's product_id.


I was thinking making a sum of all the quantities under the option table and using them to update the product table quantity value but I'm not sure the best way to do this using php in mysql?

  • 写回答

2条回答 默认 最新

  • dongshi3061 2013-05-09 08:03
    关注
    UPDATE Product
    SET Product.Quantity=(
    SELECT SUM(product_option_value.Quantity)
    FROM product_option_value
    WHERE product_option_value.product_id=Product.ProductID
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程