Reputation: 4801
I want to skip the foreach
loop;
How can I get the sum
by using a yii query?
$sql = 'select size_kb from comp_arch_stats where company_id = ' . ($model->company_id) . ' and arch_month = ' . $month . ' and arch_year = ' . $year . ';';
$val = Yii::app()->db->createCommand($sql)->queryAll();
$sum = 0;
foreach ($val AS $result) {
$sum += $result['size_kb'];
}
Upvotes: 0
Views: 239
Reputation: 1871
Try this
$criteria=new CDbCriteria;
$criteria->select = 'sum(size_kb) AS KbCount';
$criteria->condition ="company_id = :company_id AND arch_month =:arch_month AND arch_year=:arch_year "
$criteria->params = array (
':company_id '=> $model->company_id,
':arch_month' => $month,
':arch_year' => $year,
);
comp_arch_stats::model()->findAll($criteria);
Upvotes: 0
Reputation: 36
"'select sum(size_kb) as size_kb from comp_arch_stats where company_id = ' . ($model->company_id)"
Upvotes: 1