5

我正在使用以下内容尝试以如下格式输出我的每月总计:

 January
 Quoted Total : £678
 Parts Total : £432
 Profit Total : £244

 February
 Quoted Total : £650
 Parts Total : £345
 Profit Total : £123

ETC..........

    // 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>';

我遇到的问题是它只输出运行总计,所以我在页面底部一次得到以下内容,但仅此而已:

Work Complete Totals
Quoted Total : £1460.00

Parts Total : £541.43

Profit Total : £918.57

如果我在 phpmyadmin 中运行上述查询,我​​会得到结果:

pricequotedtotal    partpricetotal  profittotal        month
      1460              541.43      918.5699999999998   NULL

我的桌子的布局如下:

id  customerID  name    facebookuserurl tel email   address itemforrepair   repairdetails   otherdetails    pricequoted partprice   profit  datepartordered jobstatus   dateofcompletion    datecreated itemnumber

下面是一个示例行:

49  37ac4   Ellen Frost https://www.facebook.com/ellen.mccormick.18             Galaxy S3 (Fullsize) Blue   Broken front glass and also digitiser not working.  Quoted customer on whole lcd, digitiser assembly r...   140 114.98  25.02   2013-05-02  complete    2013-05-08  2013-05-01  251258104217

编辑 >> 下面是表格中几行的屏幕截图。 在此处输入图像描述

编辑>>

这是我的表结构: 在此处输入图像描述

4

4 回答 4

6

你需要改变这个

DATE_FORMAT('%Y-%m', completeddate)

DATE_FORMAT(completeddate, '%Y-%m')

这就是为什么你进入NULL你的month专栏,因此只有一行。

请参阅手动输入

于 2013-05-29T08:51:13.107 回答
5

伊恩,

我想以下查询会对您有所帮助。

SELECT SUM(pricequoted) AS pricequotedtotal, SUM(partprice) AS partpricetotal, SUM(profit) profittotal, MONTHNAME(dateofcompletion) FROM jobdetailsWHERE jobstatus='complete' GROUP BY DATE_FORMAT(dateofcompletion, '%Y-%m') order by dateofcompletion;

    +-----------------+----------------+------------- +------------------------------+
    | 报价总额 | 部分价格总计 | 利润总额 | 月名(完成日期)|
    +-----------------+----------------+------------- +------------------------------+
    | 140 | 114.98 | 25.02 | 五月 |
    | 140 | 114.98 | 25.02 | 六月 |
    +-----------------+----------------+------------- +------------------------------+
2 行(0.00 秒)
于 2013-06-05T05:01:13.893 回答
4

那么你可以试试这个查询

SELECT SUM(pricequoted) AS pricequotedtotal,
       SUM(partprice) AS partpricetotal,
       SUM(profit) AS profittotal,
       MONTHNAME(completeddate) AS month 
           FROM `jobdetails` 
       WHERE jobstatus='complete'
           GROUP BY MONTH(completeddate)

上表中不存在列 'completeddate',而是使用 'dateofcompletion'

因此,运行查询并将它们放入while循环将产生你想要的

我也看到了你的表结构,最好用varchar,int等来格式化一个表floattext列消耗更多的内存

http://www.pythian.com/blog/text-vs-varchar/

http://nicj.net/mysql-text-vs-varchar-performance/

于 2013-06-03T12:16:20.420 回答
4
SUM(pricequoted) AS pricequotedtotal,
SUM(partprice) AS partpricetotal,
SUM(profit) profittotal,

应该

SUM(pricequoted) AS pricequotedtotal,
SUM(partprice) AS partpricetotal,
SUM(profit) AS profittotal,

你错过了一个“AS”

于 2013-05-24T08:38:47.937 回答