This question helped me curious.

The right answer demonstrates using MySQL syntax to include values of the certain range inside a database and return the end result. Will it be better done locating the outcomes inside the daterange and computing the entire in PHP? Or if the DB be utilized wherever relevant? Clearly using the example succumbed that question, it will not really matter unless of course we are speaking 1000's of orders, however for arguments sake...

Simple information, specifically for aggregation type situations, should certainly be handled around the database for multiple reasons:

  1. Less data came back within the wire in the database
  2. Database can make the most of indexing
  3. Database is faster at aggregating data, because that is what they are designed for.

Using code for example PHP is sensible only if you have really complex information or business logic that aren't handled easily with a database tool, or when utilizing logic that databases don't do effectively, for example string manipulation.

The overall guideline is one thing such as this:

  1. Return very little data as essential in the database, and apply any logic that reduces the amount of rows in the database side.
  2. Use the information came back to complete complex business logic and markup (i.e., HTML) together with your coding language.

Why can you rewrite code? SUM(somefield) WHERE (condition) provides you with exaclty the thing you need.

$rows = mysql_query('SELECT SUM(somefield) AS `sum` FROM table WHERE (condition)');
$row = mysql_fetch_array($rows);
$sum = $row['sum'];

On the other hand, you suggest to behave such as this:

$rows = mysql_query('SELECT somefield FROM table WHERE (condition)');

$sum = 0;

while ($row = mysql_fetch_assoc($rows)) {
    $sum += $row['somefield']; }

I do not understand why. -)

And that is pure for that code, indeed more information is being moved within the second situation, to title one factor.