我想生成下面的查詢使用yii2計數和:如何組由yii2
SELECT COUNT(*) AS cnt FROM lead WHERE approved = 1 GROUP BY promoter_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我會做以下事情:
$criteria = new CDbCriteria();
$criteria->select = 'COUNT(*) AS cnt';
$criteria->group = array('promoter_location_id', 'lead_type_id');
謝謝!
您可以使用create命令編寫您的mysql查詢 – Kshitiz
請將解決方案作爲答案發布。 – Cthulhu