• <tfoot id='YsQd4'></tfoot>

      <bdo id='YsQd4'></bdo><ul id='YsQd4'></ul>
    <i id='YsQd4'><tr id='YsQd4'><dt id='YsQd4'><q id='YsQd4'><span id='YsQd4'><b id='YsQd4'><form id='YsQd4'><ins id='YsQd4'></ins><ul id='YsQd4'></ul><sub id='YsQd4'></sub></form><legend id='YsQd4'></legend><bdo id='YsQd4'><pre id='YsQd4'><center id='YsQd4'></center></pre></bdo></b><th id='YsQd4'></th></span></q></dt></tr></i><div id='YsQd4'><tfoot id='YsQd4'></tfoot><dl id='YsQd4'><fieldset id='YsQd4'></fieldset></dl></div>

    1. <small id='YsQd4'></small><noframes id='YsQd4'>

        <legend id='YsQd4'><style id='YsQd4'><dir id='YsQd4'><q id='YsQd4'></q></dir></style></legend>

        如何在yii2中计数和分组

        时间:2023-10-16
            <i id='SkvzW'><tr id='SkvzW'><dt id='SkvzW'><q id='SkvzW'><span id='SkvzW'><b id='SkvzW'><form id='SkvzW'><ins id='SkvzW'></ins><ul id='SkvzW'></ul><sub id='SkvzW'></sub></form><legend id='SkvzW'></legend><bdo id='SkvzW'><pre id='SkvzW'><center id='SkvzW'></center></pre></bdo></b><th id='SkvzW'></th></span></q></dt></tr></i><div id='SkvzW'><tfoot id='SkvzW'></tfoot><dl id='SkvzW'><fieldset id='SkvzW'></fieldset></dl></div>

                <small id='SkvzW'></small><noframes id='SkvzW'>

                <tfoot id='SkvzW'></tfoot>
                  <tbody id='SkvzW'></tbody>

                  <bdo id='SkvzW'></bdo><ul id='SkvzW'></ul>
                  <legend id='SkvzW'><style id='SkvzW'><dir id='SkvzW'><q id='SkvzW'></q></dir></style></legend>
                • 本文介绍了如何在yii2中计数和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 yii2 生成以下查询:

                  I would like to generate following query using yii2:

                  SELECT COUNT(*) AS cnt FROM Lead WHERE allowed = 1 GROUP BY prompt_location_id,lead_type_id

                  我试过了:

                  $leadsCount = Lead::find()
                  ->where('approved = 1')
                  ->groupBy(['promoter_location_id', 'lead_type_id'])
                  ->count();
                  

                  生成此查询的内容:

                  SELECT COUNT(*) FROM (SELECT * FROM `lead` WHERE approved = 1 GROUP BY `promoter_location_id`, `lead_type_id`) `c`
                  

                  在 yii 1.x 中,我会执行以下操作:

                  In yii 1.x I would've done the following:

                  $criteria = new CDbCriteria();
                  $criteria->select = 'COUNT(*) AS cnt';
                  $criteria->group = array('promoter_location_id', 'lead_type_id');
                  

                  谢谢!

                  推荐答案

                  解决方案:

                  $leadsCount = Lead::find()
                  ->select(['COUNT(*) AS cnt'])
                  ->where('approved = 1')
                  ->groupBy(['promoter_location_id', 'lead_type_id'])
                  ->all();
                  

                  并将 public $cnt 添加到模型中,在我的示例中为 Lead.

                  and add public $cnt to the model, in my case Lead.

                  正如 Kshitiz 所说,你也可以只使用 yiidbQuery::createCommand().

                  As Kshitiz also stated, you could also just use yiidbQuery::createCommand().

                  这篇关于如何在yii2中计数和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Yii2 框架 facebook 和 google 使用 authclient 登录不起作用 下一篇:如何在 yii2 的模态窗口中使用 pjax 更新小部件

                  相关文章

                  <tfoot id='sKeAK'></tfoot>

                    <legend id='sKeAK'><style id='sKeAK'><dir id='sKeAK'><q id='sKeAK'></q></dir></style></legend>

                    <small id='sKeAK'></small><noframes id='sKeAK'>

                      <i id='sKeAK'><tr id='sKeAK'><dt id='sKeAK'><q id='sKeAK'><span id='sKeAK'><b id='sKeAK'><form id='sKeAK'><ins id='sKeAK'></ins><ul id='sKeAK'></ul><sub id='sKeAK'></sub></form><legend id='sKeAK'></legend><bdo id='sKeAK'><pre id='sKeAK'><center id='sKeAK'></center></pre></bdo></b><th id='sKeAK'></th></span></q></dt></tr></i><div id='sKeAK'><tfoot id='sKeAK'></tfoot><dl id='sKeAK'><fieldset id='sKeAK'></fieldset></dl></div>

                      • <bdo id='sKeAK'></bdo><ul id='sKeAK'></ul>