如何显示记录从给定的日期范围?

时间:2014-05-12 14:35:29

标签: mysql cakephp-2.0

我正在尝试使用Cakephp搜索特定日期范围内的日期数据。我将两个日期放在两个搜索框上,如 2014-05-01至 2014-05- 31

我的表格如下:

===================================
   id  |      date     |   amount
===================================
   1   |   2014-05-04  |   200
===================================
   2   |   2014-05-05  |   100
===================================
   3   |   2014-05-06  |   100
===================================
   4   |   2014-05-07  |   200
===================================
   5   |   2014-05-08  |   100
===================================
   6   |   2014-05-09  |   100
===================================
   4   |   2014-05-10  |   200
===================================
   5   |   2014-05-11  |   100
===================================
   6   |   2014-05-09  |   100
===================================
   7   |   2014-05-10  |   200
===================================
   8   |   2014-05-11  |   100
===================================
   9   |   2014-05-12  |   100
===================================
   10  |   2014-05-13 |   200
===================================
   11  |   2014-05-14 |   100
===================================
   12  |   2014-05-15 |   100

在我搜索两个日期之后,我希望我的输出按星期从星期日开始。我在下面给出了所需的输出:

=============================
     Date   |    Amount
=============================
2014-05-04  |   1000
=============================
2014-05-11  |   600
============================= 
2014-05-18  |   0
============================= 
2014-05-25  |   0
============================= 

我尝试下面的代码不是解决方案:

$weeklyData = $this->Order->find('all', array(
            'fields' => array('SUM(Order.price) as price,WEEK(Booking.arrival_date) week ,Booking.club_id'),
            'conditions' => array('Booking.club_id' => $club_info['Club']['id'], 'Booking.arrival_date >=' => '2014-05-01', 'Booking.arrival_date <=' => '2014-05-31'),
            'group' => 'Booking.arrival_date'));

I.E我想要整个月的价值以及本月的每周总和。任何想法都会对我有所帮助。感谢

1 个答案:

答案 0 :(得分:0)

您应该使用WEEK(date\[,mode\])方法获取周数,然后使用group by weeknumber子句。

可能的解决方案:

select first_day_of_week(Booking.arrival_date), sum(order.price) 
from tables 
group by week(Booking.arrival_date) 

first_day_of_week实施check here