如何在CakePHP中使用agregate Mysql函数

oma*_*ena 4 mysql cakephp

有一种方法可以使用cakephp的agregate函数吗?比如sum()或avg()和find()方法.

更新:

我错过了书中的一句话

array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group' => 'Product.type');

显示这样做的基本结构.

谢谢您的帮助

PEd*_*hur 5

在find方法调用的fields参数中,您可以传递由聚合函数处理的字段.例:

$Model->find('all',
    array(
        'anything' => array(/* */),
        'fields' => array(
            'SUM (Model.attribute) AS total',
            'OTHERFUNCTION(OModel.other_attribute) AS other'
        ),
        'otherthing' => array(/* */)
    )
);
Run Code Online (Sandbox Code Playgroud)