I have two tables table1 and table2, in table1 I am saving the user details and in table2 the ratings he has given which are multiple rows with same table1.id and ratings in rating column, but when I execute the following code it returns only one row and average of all the ratings instead of the specific user. I am little weak at queries, I guess there needs to be Select within Select , but it is CodeIgniter so am not able to do that. Please help
$this->db->select('table1.id,table1.name, table1.email, AVG(table2.rating)');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.review_id', 'inner');
$this->db->where(array('table1.status' => 1, 'table1.b_id' => $bid));
$query = $this->db->get();
return $query;
What I want is :
> id Name email AvG
>
> 1 name1 [email protected] average of ratings by this id in table2
> 2 name2 [email protected] average of ratings by this id in table2
but what I am getting is
> id Name email AvG
>
> 1 name1 [email protected] average of all ratings in table2