如何从这个结果集中得到sum和count


how to get sum and count from this result set?

Mysql查询返回类似的结果

collector amount    name
1     0.00      gihan
1     2000.00   gihan
1     2500.00   gihan
1     11502.00  gihan
2     1140.00   lasita

我想得到像一样的结果数组

collector  amount  count  name
1         16002.00   4    gihan
2          1140.00   1    lasita

知道如何循环结果集以获得匹配收集器的和,同时获得计数并将结果放入新数组中吗?

既然MySQL可以为你做这项工作,你为什么要在PHP中解决这个问题?

SELECT collector, sum(amount) as amount, count(*) as `count`, name 
FROM yourTable
GROUP BY collector, name
SELECT collector,sum(amount) as amount,COUNT(*) as count,name
FROM Table
GROUP BY  collector,name;

使用SUM和COUNT来获得直接结果。无需循环。

SELECT Collector,SUM(Amount) as Amount,COUNT(Amount) as Count,Name
FROM YourTable
GROUP BY Collector,Name