用cakephp执行查询

时间:2013-08-08 13:24:33

标签: mysql cakephp

我想执行一个查询来获取我的数据 如何用cakephp

编写此查询
select materiel_id , SUM(quantite)  from paiements  

GROUP BY
  materiel_id

ORDER BY sum(quantite)

认为,

PS:我使用cakephp 2.3.6

2 个答案:

答案 0 :(得分:2)

假设您拥有模型关系Paiement belongsTo Materiel并且您在PaiementsController中

$this->Paiement->find('all',
    array(
        'group' => array('Paiement.materiel_id'),
        'fields' => array('Paiement.materiel_id),
        'contain' => array(
            'Materiel' => array(
                'fields' => array(
                    'SUM(Paiement.quantite) AS sum'
                ), 
                'order' => array(
                        'sum' => 'desc'
                ),
            ), // Materiel
        ) // contain
    ) // end array
); // end find

答案 1 :(得分:1)

你给我们这个代码

$mat=$this->Materiel->Paiement->query("
        select *,materiel_id , SUM(paiements.quantite) as t from paiements ,materiels
        where paiements.materiel_id=materiels.id
        GROUP BY
          materiel_id
        ORDER BY sum(paiements.quantite) desc
            ");

为您的模型调整此代码