cakephp sum()单字段 [英] cakephp sum() on single field

查看:248
本文介绍了cakephp sum()单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了高和低的如何只得到一个称为点的字段。我只需要一个总数,但我可以得到的最好的是从点表中的记录列表与成员的相关记录。

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'))));


推荐答案

为什么不使用文档记录和建议的virtualField ?
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(... ))单/数组
这就是为什么你的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆