PHP question

Jeremy Bowers jerf@jerf.org
Wed, 31 Oct 2001 11:56:53 -0500


Mike Rambo, you said:
> What I think I need:

IMHO, the best thing to do here is *not* to do it in PHP... do it in
the SQL queries.  

I'm not going to read the MySQL reference for these, but you should be
able to do the following:

> *count number of records completed in the last X number of days

Should be a COUNT() function of some kind.

> *select all records completed in the last X number of days

You probably know this is a "SELECT [something] FROM [table] WHERE
date > [date arithmatic... there's a NOW function and a date add
function right in MySQL's SQL implementation, so use those]".

> *determine days it took to complete each machine

You should be able to compute that from each record, right?

"SELECT machineId, DAYS(dateEnded - dateStarted) FROM [table]"

Something like that. Again, you'll want to look up the DAYS function
and the date manipulation functions in the MySQL manual. 

> *sum number of days for all machines

"SELECT SUM([summed quantity]) FROM table WHERE ..." 

> *divide total days by number of machines in time period

"SELECT SUM([summed quantity]) / COUNT([summed quantity]) WHERE ..."

Don't recall all  the function names, but you should be able to do
this in a two or three line query in SQL. (You *may* need a sub-SELECT
statement inside the COUNT statement. I'd hope not. There may also
already *be* an average function.)

Let the database do the work... it'll do it faster.

-- 
---------------------------------------------- 
- Jeremy Bowers                jerf@jerf.org -
- Eternal Vigilence is the Price of Freedom: - 
-        http://www.jerf.org/irights         -  
----------------------------------------------