1

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

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

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

4

1 回答 1

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;
}
于 2013-06-20T09:01:43.977 回答