mariobros
mariobros

Reputation: 899

yii: select sum using query builder

I try to execute a simple query like this:

$tot = Yii::app()->db->createCommand()
                ->select('sum(field)')
                ->from('products')
                ->where('id = ' . $id)
                ->queryRow();

But $tot return me null value.

Upvotes: 3

Views: 10069

Answers (1)

Stu
Stu

Reputation: 4150

I think you need to add an alias to your sum, so something like this might work:

$tot = Yii::app()->db->createCommand()
    ->select('sum(field) as mySum')
    ->from('products')
    ->where('id = ' . $id)
    ->queryRow();

Upvotes: 6

Related Questions