簡體   English   中英

如何使用PHP或MySQL對一個表中的不同列求和,並在另一表中獲得求和結果?

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

下面是表格示例:

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 例如,為了獲得total_balance約翰,系統會自動檢查balance columnstock_entries表,其中供應商的名字是約翰和總結發現約翰的平衡,並把結果作為total_balancesupplier_details表。

我希望它在stock_entries表中執行以下類似操作:john:90 + 250 = 380,其中380 = total_balance,它將轉到Supplier_detail中的total_balance列。 其他供應商也是如此。

您正在尋找類似的東西:

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