计算MYSQL查询的列的平均值。

时间:2021-11-05 00:13:45

Ok experts...I've got a table that I am trying to calculate the average of the values in a column. Here is my lookup:

好的专家……我有一个表我要计算列中值的平均值。这是我查找:

$gameswon = mysql_query("SELECT SUM(P1_Score) AS value_sum FROM tblMatches Where P1_ID LIKE '".$playerid."'");

Any idea how I can determine the average (sum of values / total rows)?

知道如何确定平均值(值的和/总行)吗?

Thanks for your help.

谢谢你的帮助。

3 个解决方案

#1


69  

Obviously it is

显然这是

SELECT AVG(P1_Score)

#2


8  

So in your case:

所以在你的情况中:

$gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum 
                         FROM tblMatches 
                         WHERE P1_ID LIKE '".$playerid."'");

#3


3  

Try using AVG() aggregate function instead of SUM

尝试使用AVG()聚合函数代替SUM

$gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum FROM tblMatches Where P1_ID LIKE '".$playerid."' . "GROUP BY XXXX");

and XXXX is the column that you want to get average for such as player

XXXX是你想要平均的列比如player

#1


69  

Obviously it is

显然这是

SELECT AVG(P1_Score)

#2


8  

So in your case:

所以在你的情况中:

$gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum 
                         FROM tblMatches 
                         WHERE P1_ID LIKE '".$playerid."'");

#3


3  

Try using AVG() aggregate function instead of SUM

尝试使用AVG()聚合函数代替SUM

$gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum FROM tblMatches Where P1_ID LIKE '".$playerid."' . "GROUP BY XXXX");

and XXXX is the column that you want to get average for such as player

XXXX是你想要平均的列比如player