我有评级表,其中对用户给出的每个产品进行评级,我正在检索所有评级记录,但同时我想获得基于每个产品的平均评级,但我无法获得输出
询问 :
$this->db->select('ratings.*');
$this->db->select('select AVG(ratings) as avg_rating from ratings group by product_id');
$this->db->from('ratings');
$this->db->join('users','users.id=ratings.user_id');
$this->db->get()->result();
评级表
id user_id product_id rating
1 4 1 4
2 5 2 4
3 6 1 2
4 7 4 4
预期输出:
id user_id product_id rating avg rating
1 4 1 4 3
2 5 2 4 4
3 6 1 2 3
4 7 4 4 4
梦里花落0921