![](/img/trans.png)
[英]one table data insert into different table with sum and group by in mysql and php
[英]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 column
从stock_entries
表,其中供应商的名字是约翰和总结发现约翰的平衡,并把结果作为total_balance
在supplier_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.