YII2 Frame SQL fetch a field and the problem

Source: Internet
Author: User
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!

    1. Query

(new Query())->from('table')->sum('field');
    1. Ar

Article::find()->sum('field');
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.