查询后的差异输出

时间:2017-05-26 13:44:31

标签: php mysql

我有以下问题:

SELECT BIL_Date,
SUM(BIL_Rate * BIL_Quantity) AS sumRevAccomodation,
COUNT(*) AS total_nights
FROM `___BillableDatas`
WHERE BIL_HotelId = 'AAA00'
AND BIL_Date BETWEEN "2017-04-10" AND "2017-04-18"
AND BIL_Type = "Night"
AND BIL_Status != "Cancelled"
GROUP BY BIL_Date ASC

...这给了我这个结果......

+------------+--------------------+--------------+
| BIL_Date   | sumRevAccomodation | total_nights |
+------------+--------------------+--------------+
| 2017-04-10 |             285.00 |            3 |
| 2017-04-11 |             285.00 |            3 |
| 2017-04-12 |             305.00 |            3 |
| 2017-04-13 |             310.00 |            3 |
| 2017-04-14 |             205.00 |            2 |
| 2017-04-15 |             180.00 |            2 |
| 2017-04-16 |             180.00 |            2 |
| 2017-04-17 |             190.00 |            2 |
| 2017-04-18 |             190.00 |            2 |
+------------+--------------------+--------------+

但是在这段代码之后,第一个值是miss:

$fetch = $query->fetch();
$count = $query->rowCount();

if($count >= 1) {
    while ($fetch = $query->fetch(PDO::FETCH_ASSOC)) {
        $bills[] = [
            'BIL_Date' => $fetch['BIL_Date'],
            'BIL_Revenues' => $fetch['sumRevAccomodation'],
            'BIL_NightNb' => $fetch['total_nights']
        ];
    }
}

有什么问题?

http://sqlfiddle.com/#!9/7594a8/1

感谢。

1 个答案:

答案 0 :(得分:2)

您在第一行中获取第一个值,并且从不使用它,因此您将丢失它。

$fetch = $query->fetch(); // <---- HERE
$count = $query->rowCount();

if($count >= 1)