getMedian

Syntax
 mixed getMedian {
   string $table
   string $column
   optional string $where
}

The method getMedian() does not return a true arithmetic median but simply returns the value of a specific column in a specified table closest to the middle of a record set, as specified by WHERE criteria.

If the number of records in the recordset is an even number, it returns the lower of the 2 closest records.

Because the method does not perform an arithmetic operation on the returned data, it can be used on character fields as well as numeric.

The $where clause is optional, but if used, the WHERE statement must begin with the keyword WHERE, as in “WHERE name > 'A'”.

If an error occurs or a median row cannot be determined (for example the table is empty), false is returned.


Usage

/*
 * Assumed database connection in $db
*/
$table = 'zip_codes';
$column = 'population';
$where  = 'WHERE zip_code IN (80401,80402,80403)';
 
$medianPopulation = $db->getMedian($table,$column,$where);