总和标准选择/条件在yii

时间:2013-12-24 20:09:21

标签: yii

它不能说出它是什么错误但它没有正确显示。我认为它试图显示阵列,但我认为标准是对星星求和,不是吗?

    $criteria = new CDbCriteria;
    $prod_id = Yii::app()->request->getQuery('id');
    $count = Review::model()->countByAttributes(array('target_product_id'=>$prod_id));
    $criteria->select = 'sum(star) AS sum';
    $criteria->condition = 'target_product_id = $prod_id';
    $total = $model->findAll($criteria);

3 个答案:

答案 0 :(得分:2)

使用queryScalar()来解决这个问题。

        echo Yii::app()->db->createCommand("SELECT SUM(star) FROM YourStarTable where target_product_id=$prod_id")->queryScalar();

OR

        $id=2;
        $command=Yii::app()->db->createCommand();
        $command->select('SUM(star) AS sum');
        $command->from('YourStarTable');
        $command->where('target_product_id=:id', array(':id'=>$id));
        echo $command->queryScalar();

答案 1 :(得分:0)

我认为你的问题可能就在这一行

$criteria->condition = 'target_product_id = $prod_id';

应该阅读

$criteria->condition = 'target_product_id = ' . $prod_id;

答案 2 :(得分:0)

试试这个$criteria->condition = "target_product_id = $prod_id";
将单引号更改为双引号

OR

$criteria->condition = 'target_product_id = :name';
$criteria->params=array(':name'=>$prod_id);