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.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)