如何在cakephp 3中获得最近每小时/每日/每周/每年间隔的总计数总和?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在cakephp 3中获得最近每小时/每日/每周/每年间隔的总计数总和?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一张桌子如下 –

如何在cakephp 3中获得最近每小时/每日/每周/每年间隔的总计数总和?

现在我需要报告每小时,每周,每月和每年的总计数.它可能有0,但应该包含在结果中.例如,我需要如下结果 –

$hourlyResult = array(
'00:01' => '5','00:02' => '9','00:03' => '50','00:04' => '5',..............
..............
'00:55' => '95','00:56' => '0','00:57' => '20','00:58' => '33','00:59' => '5',);

$weeklyResult = array(
'SAT' => '500','SUN' => '300'
.............
.............
'FRI' => '700'
);

何在cakePHP 3中构建查询?我得到以下链接,但不能到目前为止.

GROUP BY WEEK with SQL

我做了什么 –

$this->loadModel('SeArches');   
    $searches = $this->Searches
        ->find('all')
        ->select(['created','count'])
        ->where('DATE(Searches.created) = DATE_Sub(CURDATE(),INTERVAL 1 DAY)')
        ->group(WEEK(date))
        ->hydrate(false)
        ->toArray();
    PR($searches);

解决方法

这是你如何做到的.

按年计算

$query = $this->Searches->find();
    $query = $this->Searches
        ->find()
        ->select([
            'total_count' => $query->func()->sum('count'),'year' => $query->func()->year(['created' => 'lITeral'])
        ])
        ->group(['year'])
        ->hydrate(false);

要么

$query = $this->Searches
        ->find()
        ->select(['total_count' => 'SUM(count)','year' => 'YEAR(created)'])
        ->group(['year'])
        ->hydrate(false);

按周计算

$query = $this->Searches->find();
    $query = $this->Searches
        ->find()
        ->select([
            'total_count' => $query->func()->sum('count'),'day_of_week' => $query->func()->dayOfWeek('created')
        ])
        ->group(['day_of_week'])
        ->hydrate(false);

要么

$query = $this->Searches
        ->find()
        ->select(['total_count' => 'SUM(count)','day_of_week' => 'DAYOFWEEK(created)'])
        ->group(['day_of_week'])
        ->hydrate(false);

以同样的方式获得按小时或月计的总和.
在这里你可以阅读约CakePHP > Using SQL Functionsdate and time functions in MySQL.

脚本宝典总结

以上是脚本宝典为你收集整理的如何在cakephp 3中获得最近每小时/每日/每周/每年间隔的总计数总和?全部内容,希望文章能够帮你解决如何在cakephp 3中获得最近每小时/每日/每周/每年间隔的总计数总和?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。