简体   繁体   English

如何使用PHP或MySQL对一个表中的不同列求和,并在另一表中获得求和结果?

[英]How to sum different columns from one table and get the sum result in another table using PHP or MySQL?

Below is the tables example: 下面是表格示例:

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         ?

I want to sum balance column from stock_entries table for each supplier and get the result in supplier_details table under the column total_balance . 我想从总结余额列stock_entries表中的每个供应商,得到的结果在supplier_details栏下表total_balance For instance, to get total_balance for john, the system will automatically check the balance column from stock_entries table where supplier name is john and sum the balance found for john and put the result as total_balance in supplier_details table. 例如,为了获得total_balance约翰,系统会自动检查balance columnstock_entries表,其中供应商的名字是约翰和总结发现约翰的平衡,并把结果作为total_balancesupplier_details表。

I want it perform something like this from stock_entries table, john: 90+250=380 where 380= total_balance which will go to total_balance column in supplier_detail. 我希望它在stock_entries表中执行以下类似操作:john:90 + 250 = 380,其中380 = total_balance,它将转到Supplier_detail中的total_balance列。 the same thing for the other suppliers. 其他供应商也是如此。

You're looking for something like: 您正在寻找类似的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM