Calculate average of column from MYSQL query

China☆狼群 提交于 2019-12-09 07:23:22

问题


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.


回答1:


Obviously it is

SELECT AVG(P1_Score)



回答2:


So in your case:

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



回答3:


Try using AVG() aggregate function instead of 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



来源:https://stackoverflow.com/questions/8752705/calculate-average-of-column-from-mysql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!