Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii: select sum using query builder

Tags:

yii

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.

like image 238
mariobros Avatar asked Apr 07 '26 16:04

mariobros


1 Answers

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();
like image 97
Stu Avatar answered Apr 11 '26 01:04

Stu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!