0

如何使用 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',));

我想按Answer对结果进行分组和求和。

4

1 回答 1

1

我认为您的子查询alias需要GROUP 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');

或者

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'); 

    }
}
于 2013-07-11T17:28:14.803 回答