计算总平均评分php

时间:2019-11-29 06:28:46

标签: php rating-system

如何计算平均(用户)总评分

示例:用户评分20平均4.3

$result=mysqli_query($connection,"SELECT * FROM rating where to_user_id='".userid."'"); 

计数

$rowcount=mysqli_num_rows($result);
  echo  $rowcount; // Result 20 ratings

评分

   while($row = mysqli_fetch_array($result)){
                    echo  $row['ratting'];// 1.0 2.0 4.0.......

                    ?>

我的问题如何计算总平均值

  example like a total average of 4.3

请帮助我

6 个答案:

答案 0 :(得分:1)

您可以在MySQL中实现所有这些目标:

SELECT to_user_id, COUNT(rating) AS total_ratings, AVG(rating) AS average_rating FROM rating WHERE to_user_id = 123 GROUP BY to_user_id

echo "{$row['to_user_id']} has an average rating of {$row['average_rating']} from a total of {$row['total_ratings']} rating(s)";

答案 1 :(得分:0)

考虑使用mysql AVG函数并按用户分组

SELECT AVG(rating) _avg from ratings group by user;

https://www.w3schools.com/sql/func_mysql_avg.asp

答案 2 :(得分:0)

您可以尝试以下方法:

$average = array_sum($row['ratting']) / count($row['ratting']);

print_r($average);

答案 3 :(得分:0)

选择(求和(计数)/计数(计数))作为平均FROM评分;

添加分数并对其进行计数,其余部分负责SQL。

答案 4 :(得分:0)

如果有帮助,请使用db-fiddle-我们可以直接解决您的问题

答案 5 :(得分:-1)

您好,您可以使用此功能:-

$result=mysqli_query($connection,"SELECT AVG(rating) as average_rating FROM rating where to_user_id='".userid."'");