Does the YII2 frame have a SQL sum method that can query the sum of a field directly based on the SQL criteria? For example I want to get the Commentnum field and value of type= ' IsOpen ' in my db_article_list article table! Or if you write SQL yourself, how do you customize SQL through the framework?
Reply content:
Does the YII2 frame have a SQL sum method that can query the sum of a field directly based on the SQL criteria? For example I want to get the Commentnum field and value of type= ' IsOpen ' in my db_article_list article table! Or if you write SQL yourself, how do you customize SQL through the framework?
Or do your own writing sql convenience, as follows is the implementation of custom sql examples, you take a look at
$sql = "SELECT SUM(commentNum) AS num FROM db_article_list WHERE `type`='isOpen'";$commentNum = Yii::$app->db->createCommand($sql)->queryOne();
You can change the select.
$model->find ()->select ([' num ' = ' SUM ' (commentnum) ')->where ([' type ' = ' isOpen '])->one ();
Of course there is a sum way!
Query
(new Query())->from('table')->sum('field');
Ar
Article::find()->sum('field');