如何使用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',));
我想通过答案对结果进行分组和汇总。
答案 0 :(得分: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');
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');
}
}