计算他的总评分 php

calculate total avarage Rating php

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

示例:用户评分 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

请帮帮我

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

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

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

你可以试试这个:

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

print_r($average);

SELECT (sum(rating)/count(rating)) as average FROM rating ;

将分数相加并计数,剩下的负责SQL。

您好,您可以使用这个:-

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

您可以在 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)";