yii2框架里面有没有sql的sum方法可以直接根据sql条件查询某个字段的总和?比如我要获取我的db_article_list文章表中type='isOpen'的commentNum字段和值!或者说如果自己写sql,如何通过框架执行自定义的sql呢!
还是执行自己写的sql
方便,如下是执行自定义的sql
的例子,你参看看看
$sql = "SELECT SUM(commentNum) AS num FROM db_article_list WHERE `type`='isOpen'";
$commentNum = Yii::$app->db->createCommand($sql)->queryOne();
改一下select就可以了
$model->find()->select(['num'=>'SUM(commentNum)'])->where(['type'=>'isOpen'])->one();
当然有sum
方法!
Query
(new Query())->from('table')->sum('field');
AR
Article::find()->sum('field');