如何获得每月的总销售额

时间:2017-09-29 12:26:43

标签: php mysql sql

我试图在一个名为Flot& amp;的jquery酒吧上展示每个月的销售情况。这是我的查询

$q = $db->getRows("SELECT date AS dates,
COUNT(id) AS sales FROM orders 
WHERE date BETWEEN ? AND ? 
GROUP BY `date` ORDER BY `date`", 
PDO::FETCH_ASSOC, ["$year-01-01", "$year-12-31"]);

以下是我如何使用我的查询

$views = array_fill(1, (int) 12, 0);

foreach ($q as $viewed) {
        $the_day = (int) substr($viewed['dates'], -2);  // get the day from the date

        $views[$the_day] = $viewed['sales'];
}
 return $views;

这是我的代码

的回复
Array
(
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 0
    [0] => 1
)

它应该显示最后的[0] => 1个值进入第9个月。 我在这个查询中哪里错了?

1 个答案:

答案 0 :(得分:1)

您可以直接从数据库中获取值,例如:

String