2017-05-26 50 views
-1

我有以下查询:差分输出

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 | 
+------------+--------------------+--------------+ 

但此代码后的第一个值是小姐:

$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

感谢。

+0

这就是所有的代码? – Strawberry

+0

在第二部分之后,“$ bills”不包含“2017-04-10”的信息。 –

+2

'// $ fetch = $ query-> fetch();'注释此行并尝试 – Bhaskar

回答

2

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

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

if($count >= 1)