在cakePhp双组

时间:2013-07-11 17:17:37

标签: php sql cakephp group-by

如何使用cakePhp find()条件重现以下查询?

SELECT a, SUM(bTotal) as fullTotal FROM
  (SELECT a, SUM(b) * c as bTotal FROM Table1
  GROUP BY a)
GROUP BY a

一般来说,如何在cakePhp中编写嵌套组?

修改

我可以通过以下方式用单一组编写:

$results = $this->Vote->find('all', array('fields' => array('Choice.Answer', '(COUNT(Vote.id) * User.voteMultipler) AS voteTotal', 'choice_id', 'user_id'),
                                          'group' => 'choice_id, Vote.user_id',));

我想通过答案对结果进行分组和汇总。

1 个答案:

答案 0 :(得分:1)

我认为子查询需要再次aliasGROUP BY

SELECT q.a, SUM(q.bTotal) as fullTotal FROM
  (SELECT a, SUM(b) * c as bTotal FROM Table1
  GROUP BY a) q
GROUP BY q.a

在cakephp中

$this->YourModel->query(' SELECT q.a, SUM(q.bTotal) as fullTotal FROM
      (SELECT a, SUM(b) * c as bTotal FROM Table1
      GROUP BY a) q
    GROUP BY q.a');

OR

App::uses('AppModel', 'Model');
class MYModel extends Model {

    public function myfunction()
    {
        return $this->query(' SELECT q.a, SUM(q.bTotal) as fullTotal FROM
          (SELECT a, SUM(b) * c as bTotal FROM Table1
          GROUP BY a) q
        GROUP BY q.a'); 

    }
}