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

How to get sum of total counts with most recent hourly / daily / weekly / yearly interval in cakephp 3?

我有一个 table 如下-

现在我需要制作每小时、每周、每月和每年的总计数报告。它的总和可能为 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 中构建查询?我得到以下 link 但不能走这么远。

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 Functions and date and time functions in MySQL.

的信息