2012-03-28 138 views
0

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 

任何想法如何循環的結果集,以獲取和匹配收藏家和同時獲得計數,並把結果成一個新的數組?

回答

3

爲什麼你想在PHP中解決這個問題,當你可以讓MySQL爲你做這些工作?

SELECT collector, sum(amount) as amount, count(*) as `count`, name 
FROM yourTable 
GROUP BY collector, name 
+0

非常感謝答案 – 2012-03-28 13:57:32

1
SELECT collector,sum(amount) as amount,COUNT(*) as count,name 
FROM Table 
GROUP BY collector,name; 
+0

非常感謝答案 – 2012-03-28 13:57:51

1

使用SUM和COUNT獲得的直接結果。沒有必要的循環。

SELECT Collector,SUM(Amount) as Amount,COUNT(Amount) as Count,Name 
FROM YourTable 
GROUP BY Collector,Name 
相關問題