我想执行一个查询来获取我的数据如何用 cakephp 编写这个查询
select materiel_id , SUM(quantite) from paiements
GROUP BY
materiel_id
ORDER BY sum(quantite)
认为,
PS:我使用 cakephp 2.3.6
假设你有模型关系 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
你可以给我们这个代码
$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
");
为您的模型调整此代码