开发者

How do I SUM total between date and status

Current db structure 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  

How do I sum pending & paid payment between current month & status?

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

Results for August 2011 should be

 pending = 800
 paid    = 1320


You'd need a group by clause:

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


Take out the begin, end and status and you're there.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜