PHP MySQL Aggregate SUM
From w3cyberlearnings
Contents |
PHP MySQL Aggregate Function SUM()
The Aggregate Function SUM() adds all the rows.
Syntax
SELECT SUM(salary) as total_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 SUM(salary) as total_salary FROM employee_salary"; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo mysql_result($result, 0); mysql_close($connection); ?>
Output
388