如何在日期和状态之间汇总总和

时间:2011-08-25 20:27:35

标签: php mysql

当前数据库结构payment

ID pending  paid    begin_work  end_work    status
----------------------------------------------------
1  100      20      2011-08-01  2011-08-30  pending 
2  200      1000    2011-08-01  2011-08-15  pending 
3  500      100     2011-08-05  2011-08-25  pending
4  0        200     2011-07-1   2011-08-25  paid  

如何对待待处理的&当月和之间的付款状态?

$qm = $db->query("
   SELECT
   begin_work, end_work, status,  
   SUM(pending) AS mpending, 
   SUM(paid) AS mpaid
     FROM payment
    ")  

August 2011的结果应为

 pending = 800
 paid    = 1320

3 个答案:

答案 0 :(得分:0)

取出beginendstatus,你就在那里。

答案 1 :(得分:0)

您需要group by子句:

SELECT begin_work, end_work, status, SUM(pending), SUM(paid)
FROM payment
GROUP BY YEAR(begin_work), MONTH(begin_work)

答案 2 :(得分:0)

SELECT
   begin_work, status,  
   SUM(pending) AS mpending, 
   SUM(paid) AS mpaid
FROM payment
where MONTHNAME(begin_work) = MONTHNAME(CURDATE())
group by status