2013-07-11 66 views
0

我該如何重現以下查詢cakePhp find() and conditionsDouble group by inPhp

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

我想你的子查詢需要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'); 

    } 
} 
+0

但它的SQL,我想改寫它作爲CakePHP的命令。 – boj

+0

查看我的更新回答 –

+0

技術上還是sql imo –