Hey guys.

I’m wondering: how to get the average of a column in my database table displayed? And furthermore, how to display the average of a column for each row that has a certain integer in an another column?

Basically like a rating system, in which i have a rating column, a video column and a user column. I want to find the average rating for a certain video.

Can anybody help me?

Btw, i’ve already looked at the Statistical query part of The Guide, but i doesn’t understand it fully…

It would help with an example :)

class Video extends CActiveRecord {

  public function relations() {

    return array(

      //'relation name' => array(self::STAT, 'table name', 'related column', query options)

      'averageRating' => array(self::STAT, 'video_rating', 'video_id' 'select' => 'AVG(rating)'),




Should give you some ideas.

Thanks, it worked like a charm :)

Now, just another question: What if i have multiple things of each video i want to be able to rate, but i don’t want to have more columns in my table besides an integer column that detects what kind of thing you are rating (like quality=1, acting=2 etc.)? So to display a rating, i’ll want to ask the database to give me a rating with a certain id, and a certain type.

I’m guessing i have to add a condition to the relation, such as ‘condition’ => ‘type=$type’ and then somehow get $type on the specific page, but i don’t know how to do that.