0

我想跳过foreach循环;

如何sum使用 yii 查询获得?

$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'];
        }
4

2 回答 2

1

“'选择 sum(size_kb) as size_kb from comp_arch_stats where company_id = ' . ($model->company_id)”

于 2013-08-19T13:58:35.480 回答
0

尝试这个

$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);
于 2013-08-19T14:09:44.570 回答