1- Examples of use
SELECT SUM(column) AS thesum FROM tablename WHERE condition.
The above will return thesum being the sum of all numeric data stored in "field".
So assuming we came across 50 rows (Or Zero rows), what would the result of a PHP mysql_num_rows be in that case, will it be as many as the number of rows we came across ?
Not really, If there were actually any rows in the above, you will always get a 1.
If there were no rows, You will get a..... 1 again
In short, mysql_num_rows will always return a 1 for the above SQL.
So, let us introduce you better to the MySQL SUM
* To find the sum of a certain column grouped by another column
SELECT columnone, sum(columntwo) as thesum FROM `tablename` GROUP BY columnone
What the above will result in is that the sum will be calculated once for each and every unique (Distinct) columnone, so if we can devide all rows into 3 groups with the columnone value equal among all rows of every group, we will get 3 sums, and PHP's mysql_num_rows will return 3.
* What happenes if i omit the AS in the SQL ?
You will need to refer to the sum as sum(column) when you need to obtain it.
* What happens if i try to sum a varchar column ?
You might expect an error, in reality you will get a 0
* Can't i just select them and use PHP to SUM them ? then i can now the number of rows that have been summed ?
You can, But it comes at an overhead in memory and processor, but mostly a memory penalty
![]() |
|
![]() |
|
| .COM | $ 8.49 |
| .NET | $ 6.49 |
| .ORG | $ 7.49 |
| .INFO | $ 1.99 |
| Compare Pricing | |
| Cheap Domains | |