Yii如何从Count和Group By获取数据

时间:2013-06-20 08:52:12

标签: yii

如何通过在Yii中执行此查询来获取数组?

SELECT `sevrity_id`,COUNT(*) FROM `Incident` GROUP BY `sevrity_id`

我需要一个这样的数组:array(1=>20,2=10,3=12)

1 个答案:

答案 0 :(得分:2)

public function getSevrityCounts()
{
    $data = array();
    $command = Yii::app()->db->createCommand('SELECT sevrity_id,COUNT(*) AS num FROM Incident GROUP BY sevrity_id');
    foreach($command->queryAll() as $row) {
        $data[ $row['sevrity_id'] ] = $row['num'];
    }
    return $data;
}