武天老施 2012-02-21 19:55:57 14142次浏览 3条回复 2 1 0

请教各位大侠,怎么对数据库查询进行group by 和sum() 操作啊,除了使用SQL语句,还有别的方法么

觉得很赞
  • 回复于 2012-03-13 20:24 举报

    用AR方式实现:
    1.直接用findall
    $result = Test::model()->findAll(array('select'=>'name, sum(record) as summary','group'=>'category'));
    2.在model relation里声明一个聚合数据

    public function relations() {
        'childrenSum'=>array(self::STAT,'Test','pid','select'=>'sum(pid)'),
    }
    
  • 回复于 2012-03-13 20:34 举报

    楼上说的没错!

  • 回复于 2012-03-14 15:31 举报

    这样可以做到。

您需要登录后才可以回复。登录 | 立即注册