[英]Get average of average columns in mysql
我有一張存儲餐廳評級的桌子。 如下圖所示。
我試圖獲得我成功的所有這些列的平均值,但我也想要所有這些平均值的平均值作為主要平均值。
我嘗試了以下查詢,但平均評分為 3,這是不准確的。 我認為 mysql 正在向我返回最終結果的整數值。
return $this->db->select('((ambience + music + service + value_for_money + cleanliness + sanitary + view)/7) as rating, AVG(ambience) as ambience, AVG(music) as music, AVG(service) as service,AVG(value_for_money) as value_for_money, AVG(cleanliness) as cleanliness, AVG(sanitary) as sanitary, AVG(view) as view' )
->where('restaurant_id',$restaurantId)
->get('restaurant_ratings')
->row_array();
當我運行上述查詢時,我得到 3 作為評級字段的平均值。
實際結果為 3.42。
請幫助我了解我做錯了什么以及可以做些什么來獲得准確的結果。 謝謝
只需添加AVG
即可計算評分:
$query =
'AVG((
ambience +
music +
service +
value_for_money +
cleanliness +
sanitary +
view
)/7) as rating,
AVG(ambience) as ambience,
AVG(music) as music,
AVG(service) as service,
AVG(value_for_money) as value_for_money,
AVG(cleanliness) as cleanliness,
AVG(sanitary) as sanitary,
AVG(view) as view';
return $this->db
->select($query)
->where('restaurant_id',$restaurantId)
->get('restaurant_ratings')
->row_array();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.