Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate average of column from MYSQL query

Tags:

mysql

sum

average

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

like image 813
DoubleA Avatar asked Jan 06 '12 02:01

DoubleA


People also ask

How do I get the average of a column in MySQL?

MySQL AVG() Function The AVG() function returns the average value of an expression.

How do you calculate average in SQL query?

The avg() function has the following syntax: SELECT AVG( column_name ) FROM table_name; The avg() function can be used with the SELECT query for retrieving data from a table.

What is AVG in MySQL?

The AVG() function returns the average value of an expression. Note: NULL values are ignored.


2 Answers

You can use AVG like so:

SELECT AVG(P1_Score) 
like image 67
zerkms Avatar answered Oct 06 '22 17:10

zerkms


So in your case:

$gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum                           FROM tblMatches                           WHERE P1_ID LIKE '".$playerid."'"); 
like image 33
xQbert Avatar answered Oct 06 '22 16:10

xQbert