如何在cakephp 3中获得最近每小时/每天/每周/每年间隔的总计数之和?
我有一张如下的桌子- 现在,我需要在每小时,每周,每月和每年的总计数报告。它的总和可能为0,但应包含在结果中。例如,我需要如下结果-如何在cakephp 3中获得最近每小时/每天/每周/每年间隔的总计数之和?,php,mysql,cakephp,cakephp-3.x,Php,Mysql,Cakephp,Cakephp 3.x,我有一张如下的桌子- 现在,我需要在每小时,每周,每月和每年的总计数报告。它的总和可能为0,但应包含在结果中。例如,我需要如下结果- $hourlyResult = array( '00:01' => '5', '00:02' => '9', '00:03' => '50', '00:04' => '5', .............. .............. '00:55' => '95', '00:56' => '0', '00:57' =>
$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中构建查询?我得到了下面的链接,但不能走这么远
我所做的-
$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()
->select(['total_count' => 'SUM(count)', 'day_of_week' => 'DAYOFWEEK(created)'])
->group(['day_of_week'])
->hydrate(false);
或
$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()
->select(['total_count' => 'SUM(count)', 'day_of_week' => 'DAYOFWEEK(created)'])
->group(['day_of_week'])
->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();
$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()
->select(['total_count' => 'SUM(count)', 'day_of_week' => 'DAYOFWEEK(created)'])
->group(['day_of_week'])
->hydrate(false);
同样的方法,你可以得到总金额按小时或月。
在这里您可以阅读和。
Raw
query可以吗?我想知道cakephp的方法。不过,我也不知道原始查询。好的。然后你必须等到一个蛋糕php
的家伙过来。如果你不介意,请告诉我原始查询方式,因为cakephp也支持原始查询。如果我没有找到任何cakephp的家伙,那么我将以原始方式实现。然后我需要更多信息:您的预期输出是什么样的?你所说的每周是指一周/最后7天吗?事实上,您的预期输出将回答许多问题。