如何在cakephp中使用聚合函数SUM?

前端之家收集整理的这篇文章主要介绍了如何在cakephp中使用聚合函数SUM?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个查询,我想用数量的总和来订购.我必须在分页中使用此结果,请帮帮我..
SELECT (SUM( OrderItem.quantity )) AS qty,`OrderItem`.`item_name`,`OrderItem`.`size` FROM `order_items` AS `OrderItem` WHERE 1 =1 
GROUP BY `OrderItem`.`item_name`,`OrderItem`.`size` 
ORDER BY (SUM( OrderItem.quantity )) DESC

我已经尝试了下面的代码,但它不起作用

$this->paginate = array('fields'=>array('(SUM(OrderItem.quantity)) as qty','OrderItem.item_name','OrderItem.size'),'group'=>array('OrderItem.item_name','order' => array('(SUM(OrderItem.quantity))'=>'DESC'));
您可以使用“自定义查询分页¶”,如下所示:

http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#custom-query-pagination

在您的模型中使用它:

public function paginate($conditions,$fields,$order,$limit,$page = 1,$recursive = null,$extra = array()) {
        $page--;
        $offset=$limit*$page;
        $result= $this->find('all',array('fields'=>array('(SUM(OrderItem.quantity)) as qty','order' => array('qty'=>'DESC'),'conditions'=>$conditions,'limit'=>$limit,'offset'=>$offset));
       return $result;

    }
原文链接:https://www.f2er.com/php/134323.html

猜你在找的PHP相关文章