duangonglian6028 2018-03-04 19:56
浏览 94
已采纳

从mysql获取数据并在几周内对它们进行分组,并显示日期

I am printing data on weekly basis. I need to add one thing to my query. I want the starting and ending date of the week as well along with data.

At the moment the data is like this

        [0] => Array
            (
                [WEEK] => 7

                [total_sub_total] => 110.30
                [total_tax] => 9.92
                [total_restaurant_delivery_fee] => 0
            )


        [0] => Array
            (
                [WEEK] => 8

                [total_sub_total] => 6540.00
                [total_tax] => 1046.40
                [total_restaurant_delivery_fee] => 0
            )

I want the dates as well of that week. for example, it should be

[0] => Array
                (
                    [WEEK] => 8,
                    [starting_week_date] => 2018-02-21 13:18:10,
                    [ending_week_date] => 2018-02-26 13:18:10,


                    [total_sub_total] => 6540.00
                    [total_tax] => 1046.40
                    [total_restaurant_delivery_fee] => 0
                )

Here is the code

public function getWeeklyEarnings($restaurant_id)
    {


        return $this->find('all', array(


            //'contain' => array('OrderMenuItem', 'Restaurant', 'OrderMenuItem.OrderMenuExtraItem', 'PaymentMethod', 'Address','UserInfo','RiderOrder.Rider'),

            'conditions' => array(



                'Order.restaurant_id' => $restaurant_id,
                'Order.status' => 2,
                'Order.created >    DATE_SUB(NOW(), INTERVAL 4 WEEK)'



            ),
            'fields' => array(


                'WEEK(Order.created) AS WEEK',

                'sum(Order.sub_total)   AS total_sub_total',
                'sum(Order.tax)   AS total_tax',
                'sum(Order.restaurant_delivery_fee)   AS total_restaurant_delivery_fee',

            ),
            'group' => array('WEEK(Order.created)'),

            'recursive' => 0

        ));
    }
  • 写回答

1条回答 默认 最新

  • doutang2017 2018-03-05 03:49
    关注

    There is no simple function that will yield a week interval (or start/end date of a week) given a week number. You have to find these dates manually.
    try this :

    'fields' => array(
        'WEEK(Order.created) AS WEEK',
    
        'DATE_ADD(Order.created, INTERVAL(1-DAYOFWEEK(Order.created)) DAY) AS week_start',
        'DATE_ADD(Order.created, INTERVAL(7-DAYOFWEEK(Order.created)) DAY) AS week_end',
    
        'sum(Order.sub_total)   AS total_sub_total',
        'sum(Order.tax)   AS total_tax',
        'sum(Order.restaurant_delivery_fee)   AS total_restaurant_delivery_fee'
    )
    

    The DAYOFWEEK() function returns an integer ranging from 1 (Sunday) to 7 (Saturday). So if Order.created happens to be Tuesday we get the following statements:

    DATE_ADD(Order.created, INTERVAL -2 DAY)

    which essentially means “subtract 2 days from Order.created (which is that week’s Sunday) and also:

    DATE_ADD(Order.created, INTERVAL 4 DAY)

    which yields the date of that week’s Friday.

    Or, you can try :

    'fields' => array(
        'WEEK(Order.created) AS WEEK',
    
        'MIN(Order.created) as week_start',
        'MAX(Order.created) as week_end',
    
        'sum(Order.sub_total)   AS total_sub_total',
        'sum(Order.tax)   AS total_tax',
        'sum(Order.restaurant_delivery_fee)   AS total_restaurant_delivery_fee'
    )
    

    But you have to be sure that you have orders on every single day of the week.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教