nguyendh
(Duynguyen0511)
1
I wonder if I m missing something about sum function ( http://www.yiiframework.com/doc-2.0/yii-db-query.html#sum()-detail )
If the result from sum is null, I assign 0 to it as demonstrate below
$totalValue = Book::find()->where(['type' => 'Fiction'])->sum('[[value]]');
$totalValue = ($totalValue == null) ? 0 : $totalValue;
Is there a way to built in the default to 0 if null within sum() ?
hrnair
(Harikrishnan Hr)
2
$totalValue = Book::find()->where([‘type’ => ‘Fiction’])->sum(‘case when value
=’’ then 1 else 0 end’);
nguyendh
(Duynguyen0511)
3
your code doesnt do. I want if the sum is null, make it 0, else, return the sum.
hrnair
(Harikrishnan Hr)
4
How about
$totalValue = (int) Book::find()->where(['type' => 'Fiction'])->sum('[[value]]');