【问题标题】:How to display Records From a given date range?如何显示给定日期范围内的记录?
【发布时间】:2014-06-29 23:37:01
【问题描述】:

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

我的桌子如下所示:

==================================== 编号 |日期 |数量 ==================================== 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

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

============================== 日期 |数量 ============================== 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 我想要整个月的价值以及本月每周的总和。任何想法都会对我有很大帮助。谢谢

【问题讨论】:

  • 我已经更新了代码。请看

标签: mysql cakephp-2.0


【解决方案1】:

您应该使用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

【讨论】:

  • 你能写出完整的查询吗?
  • @Vibration 有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多