PHP MySQL Aggregate AVG
From w3cyberlearnings
Contents |
PHP MySQL Aggregate Function AVG()
The Aggregate Function AVG() returns the average value in the row.
Syntax
Return average hourly salary.
SELECT AVG(salary) FROM employee_salary;
Example 1
<?php define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $connection = mysql_connect(HOST, USER, PASS) or die("can not connect to the server!<br/>"); $rdb = mysql_select_db(DBNAME) or die("The " . DBNAME . "database could not be selected"); $sel_query = "SELECT AVG(salary) FROM employee_salary"; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo mysql_result($result, 0); mysql_close($connection); ?>
Output
38.8