Akila Wickramasekara
Akila Wickramasekara

Reputation: 47

Yii how to get Data from Count and Group By

How to get an array by Executing this query in Yii?

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

I need an array like this: array(1=>20,2=10,3=12)

Upvotes: 1

Views: 1261

Answers (1)

Michael Härtl
Michael Härtl

Reputation: 8587

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;
}

Upvotes: 2

Related Questions