单个字段上的 cakephp sum()

标签 cakephp

我搜索了很多关于如何获得一个名为 points 的字段的总和。我只需要一个总数字,但我能得到的最好结果是 Points 表中的记录列表以及来自成员(member)的相关记录。

    $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

$this->Member->Point->virtualFields['total'] = 'SUM(Point.points)';
$totalPoints = $this->Member->Point->find('all', array('fields' => array('total')));

这样更干净。

另请注意您在 $options 数组 (...find('all', array(array(...) 中获得的双数组。以及我如何仅使用一个/平面阵列。 这就是为什么您的 SUM() 调用作为字段不起作用的原因。

关于单个字段上的 cakephp sum(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693008/

相关文章:

php - 如何在 CakePHP 3 中使用另一个表更新一个表

cakephp - Ubuntu 14.04 中 CakePHP shell 的问题

forms - CakePHP 2.3.1 在某些 View 中停用表单验证

php - 尝试将 CakePHP Find() 转换为 MySql 语句

cakephp - CakePHP 中的自动登录

javascript - 从外部 View 模型调用 Knockout.js 中的函数

mysql - 房间预订sql查询

mysql - 来自 SQL 的 CakePHP 子查询

cakephp - TCPDF QR Code 一直不一样

php - CakePHP 中的单元测试?