Cakephp 3中的SUM查询不起作用 [英] SUM query in cakephp 3 not working
本文介绍了Cakephp 3中的SUM查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加同一字段的数据,并想返回一个结果,我使用了以下查询:
I am trying to add the data of same field and want to return a result i used the following query:
$total = $this->Details->find('all', array(
'fields' => array('sum(Details.total_downtime+ Details.total_downtime)'),
'conditions' => array('Details.site_id' => $id)
));
print_r($total->toArray());
exit;
我得到以下结果:
Array (
[0] => App\Model\Entity\Detail Object (
[displayField] => username
[_accessible:protected] => Array (
[*] => 1
[id] => 1
[site_id] => 1
[uptime] => 1
[downtime] => 1
)
[_properties:protected] => Array (
[sum(Details] => Array ( [total_downtime+ Details] => 4 )
)
[_original:protected] => Array ( )
[_hidden:protected] => Array ( )
[_virtual:protected] => Array ( )
[_className:protected] => App\Model\Entity\Detail [_dirty:protected] => Array ( )
[_new:protected] =>
[_errors:protected] => Array ( )
[_registryAlias:protected] => Details
)
)
在哪里可以找到我的金额
?
推荐答案
我想您正在尝试实现这样的目标?
I guess you are trying to achieve something like this?
$query = $Details->find();
$query
->select(['sum' => $query->func()->sum('Details.total_downtime')])
->where(['Details.site_id' => $id])
->toArray();
这篇关于Cakephp 3中的SUM查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文