MySQL-获取基于同一表上相同文件的列值的总和


MySQL - get the sum of the column value(s) based on same filed on the same table

我有一个名为tbl_collection的mysql表和另一个具有客户名称的表。我加入了一个内部联盟。加入效果很好。

这是我的tbl_collection

+-----------+------------+---------------+
| customer  |    date    |    ach_val    | 
+-----------+------------+---------------+
|     30002 | 2012-02-02 | 200           |
|     30002 | 2012-02-05 | 250           | 
|     30002 | 2012-02-06 | 122           |
|     30003 | 2012-02-03 | 500           |
|     30004 | 2012-02-04 | 425           |
|     30004 | 2012-02-06 | 225           |
|     30004 | 2012-02-10 | 300           |
+-----------+------------+---------------+

我想要的是得到每个客户每个月ach_val的总和。

例如,2012-02年每个客户的ach_val之和。

30002的(ach_val)=200+250+122=572

30003的(ach_val)=500=500

30004的(ach_val)=425+225+300=950

这就是我想做的。

$r = mysql_query("select tbl_collection.customer, sum(tbl_collection.col_ach) as coll from tbl_collection inner join tbl_mas_customer on tbl_mas_customer.customer = tbl_collection.customer where rep = '503' and DATE_FORMAT(date, '%Y-%m') = '2012-02'");
select customer,sum(ach_val) from tbl_collection group by customer,month(date);