【问题标题】:How to sum different columns from one table and get the sum result in another table using PHP or MySQL?如何使用 PHP 或 MySQL 对一个表中的不同列求和并在另一个表中得到求和结果?
【发布时间】:2015-04-13 20:57:04
【问题描述】:

以下是表格示例:

stock_entries as table 1 
stockid  supplier    item    total   payment   balance
   1      john      iphone    100      10        90
   2     charles     itel      50      40        10
   3      john       ipad     500     250       250
   4      alex      tecno      20      20         0
   5     charles    ipad       30      0         30

supplier_details as table 2
number    supplier    contact    total_balance
  1        alex       0843433         ?
  2       charles     7784336         ?
  3        john      21184584         ?

我想对每个供应商的stock_entries 表中的余额列求和,并在supplier_details 表中的total_balance 列下得到结果。 例如,要获取 john 的total_balance,系统会自动检查供应商名称为 john 的 stock_entries 表中的 balance column 并将找到的余额为 john 并将结果作为 total_balance 放入 supplier_details 表中.

我希望它从 stock_entries 表执行类似的操作,john: 90+250=380 其中 380= total_balance 将转到供应商详细信息中的 total_balance 列。其他供应商也是如此。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你正在寻找类似的东西:

    insert into table2 (field1, field2) 
       select sum(whatever), otherfield 
       from table1 
       where otherfield in('val1', 'val2')
       group by otherfield ;
    

    【讨论】:

    • 能否请您在sql语句中使用这两个表信息,以便我理解好
    • @Dalhat,我不是在为你做这项工作,只是向你展示它的语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2019-12-21
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多