使用php计算每月总计

时间:2013-05-23 14:55:29

标签: php mysql sum

我正在使用以下代码来计算我一直在做的工作的总数。我现在想做的是每个月增加一个总额,但我不知道如何适应它这样做。我需要像

这样的东西
$query = $db->query("SELECT SUM(pricequoted) pricequotedtotal,SUM(partprice) partpricetotal,SUM(profit) profittotal FROM `jobdetails` WHERE jobstatus='complete' AND WHERE `completeddate` = ALL DATES IN JANUARY, FEB, ETC ETC"

并且每个月都这样做,所以我每个月都可以: 的一月 引用: £200 零件价格: £50 利润: £150

    // Work Complete Totals
    $query = $db->query("SELECT SUM(pricequoted) pricequotedtotal,SUM(partprice) partpricetotal,SUM(profit) profittotal FROM `jobdetails` WHERE jobstatus='complete'");
    $result = $query->fetch_object();
    $pricequoted1 = "{$result->pricequotedtotal}";
    $pricequoted = number_format($pricequoted1, 2, '.', '');
    $partprice1 = "{$result->partpricetotal}";
    $partprice = number_format($partprice1, 2, '.', '');
    $profit1 = "{$result->profittotal}";
    $profit = number_format($profit1, 2, '.', '');
    echo '<div style="float:right; padding-right:10px;">';
    echo '<strong>Work Complete Totals</strong>';
    echo '<p><strong style="color:red;">Quoted Total : &pound;'.$pricequoted.'</strong></p>';
    echo '<p><strong style="color:Darkorange ;">Parts Total : &pound;'.$partprice.'</strong></p>';
    echo '<p><strong style="color:green;">Profit Total : &pound;'.$profit.'</strong></p>';
    echo '</div>';

ID 顾客ID 名称 facebookuserurl 联系电话 电子邮件 地址 itemforrepair repairdetails 其他详情 pricequoted partprice 利润 datepartordered 工作状态 完成日期 创建日期 itemnumber

47 bf0b4 Becki Lett https://www.facebook.com/becki.lett 笔记本电脑 我的女儿笔记本电脑似乎有一个问题,我想... 好吧没问题,我应该能够适合你... 0 1970-01-01 inprogressjobstodo 1970-01-01 二零一三年四月三十零日 4558547578

2 个答案:

答案 0 :(得分:2)

稍微清理的代码版本,缺少GROUP BY语句。试试这个。

// Work Complete Totals
$query = $db->query("SELECT SUM(pricequoted) AS pricequotedtotal,
                            SUM(partprice) AS partpricetotal,
                            SUM(profit) profittotal,
                            DATE_FORMAT('%Y-%m', completeddate) AS month 
                     FROM `jobdetails` 
                     WHERE jobstatus='complete'
                     GROUP BY DATE_FORMAT('%Y-%m', completeddate)");


echo '<div style="float:right; padding-right:10px;">';
echo '<strong>Work Complete Totals</strong>';

while($result = $query->fetch_object()) {

    $pricequoted = number_format($result->pricequotedtotal, 2, '.', '');
    $partprice   = number_format($result->partpricetotal, 2, '.', '');
    $profit      = number_format($result->profittotal, 2, '.', '');

    echo '<p><strong style="color:red;">Quoted Total : &pound;'.$pricequoted.'</strong></p>';
    echo '<p><strong style="color:Darkorange ;">Parts Total : &pound;'.$partprice.'</strong></p>';
    echo '<p><strong style="color:green;">Profit Total : &pound;'.$profit.'</strong></p>';
}

echo '</div>';

答案 1 :(得分:0)

使用Mysql GROUP BY

例如

SELECT Year(record_date),MONTH(record_date),SUM(pricequoted) pricequotedtotal,SUM(partprice) partpricetotal,SUM(profit) profittotal FROM `jobdetails` 
WHERE jobstatus='complete'
GROUP BY YEAR(record_date), MONTH(record_date)

record_date将是date或datetime类型的列