单个字段上的 cakephp sum() [英] cakephp sum() on single field
问题描述
我对如何获得一个称为点的字段进行了高低搜索.我只需要一个总数,但我能得到的最好结果是 Points 表中的记录列表以及来自 Member 的相关记录.
I have searched high and low on how to just get a total of a field called points. I just need one total figure but the best I can get is a list of records from the Points table with the associated records from the Members.
$totalPoints = $this->Member->Point->find('all', array(
array('fields' => array('sum(Point.points) AS Point.ctotal'))));
推荐答案
为什么不使用文档中记录和建议的 virtualFields?http://book.cakephp.org/2.0/en/models/virtual-fields.html
Why not using virtualFields as documented and suggested by the docs? http://book.cakephp.org/2.0/en/models/virtual-fields.html
$this->Member->Point->virtualFields['total'] = 'SUM(Point.points)';
$totalPoints = $this->Member->Point->find('all', array('fields' => array('total')));
这样更干净.
还要注意您在 $options 数组中得到的双数组 (...find('all', array(array(...
)).以及我如何只使用一个/平面阵列.这就是您的 SUM() 作为字段调用不起作用的原因.
Also note the double array you got there in your $options array (...find('all', array(array(...
). And how I used only a single/flat array.
This is the reason why your SUM() call as fields does not work.
这篇关于单个字段上的 cakephp sum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!