从 MYSQL 查询计算列的平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8752705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:15:55  来源:igfitidea点击:

Calculate average of column from MYSQL query

mysqlsumaverage

提问by DoubleA

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

我有一个表,我试图计算列中值的平均值。这是我的查找:

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)?

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

回答by zerkms

You can use AVGlike so:

您可以像这样使用AVG

SELECT AVG(P1_Score)

回答by xQbert

So in your case:

所以在你的情况下:

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

回答by GiantRobot

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 是您想要获得平均值的列,例如玩家