How to count and group by in yii2(如何在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
我试过了:
生成此查询的内容:
在 yii 1.x 中,我会执行以下操作:
In yii 1.x I would've done the following:
谢谢!
推荐答案
解决方案:
并将 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中计数和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!