View Full Version : Do Some Calculations on Query Results

03-04-2007, 06:05 PM
I have got 4 tables in the same database. The name of the tables are table03, table04, table05 & table06. The structure of the tables are exactly the same. I want to calculate the median price of the price column in each table individually based on the keywords I keyed in. The keywords will query the full_add column.

So assuming that my keywords were Toyota, Honda, Mercedes and BMW(keyed in as 1 keyword per line in the form), the end result should display something like this:-
Median Price from table03
Toyota - $3883
Honda - $356
Mercedes - $7865
BMW - $1347

Median Price from table04
Toyota - $7688
Honda - $4312
Mercedes - $8709
BMW - $1254

Median Price from table05
Toyota - $8776
Honda - $3445
Mercedes - $8778
BMW - $1223

Median Price from table06
Toyota - $6578
Honda - $5445
Mercedes - $545
BMW - $12548

What I've already done succesfully is to query the database based on 1 record per line. My code is below.

The part that I do not know how to code is as follows:-
1. A join query to query all tables at one go.
2. To calculate the median 4 times(once for each table) & display the results like the sample above.

Please show me how to write the code. Thank you for your help.

$limit = "LIMIT 0,100000";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");

$emails=explode("\n", str_replace("\r", "", $_POST['femail']));
foreach($emails as $e){
$email_r[]="full_add LIKE '%".mysql_escape_string($e)."%'";
$email_str=implode(' OR ',$email_r);

$query1="SELECT price FROM table WHERE ".$email_str . $limit;

/* Median calculation for the price. */
while ( $row=@mysql_fetch_array($result1,MYSQL_NUM) ) {
if ($num0 % 2) {
$median1 = $thearray[floor($num0+1)/2 - 1];
} else {
$median1 = ($thearray[$num0/2] + $thearray[$num0/2 - 1]) / 2;

<form name="ftest" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="femail"></textarea><br />
<input type="submit" name="Submit" value="Send" />
<table width=700 border=1 height="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<td style="border-style: solid; border-width: 1" bgcolor="#99CCCC" align="left" width="350"><b>
<div align="center"><font size="3" face="Arial, Helvetica, sans-serif"><? echo "Median Price - RM<font color='#FF33cc'> $median1 </font>"; ?></font></div></td>
Additional Info:-

The table structure for all my tables are as follows:-
`id` int(6) NOT NULL auto_increment,
`date` year(4) NOT NULL default '0000',
`full_add` varchar(255) NOT NULL default '',
`status` varchar(10) NOT NULL default '',
`size` int(6) default '0',
`price` int(8) default '0',
`price_psf` double NOT NULL default '0',
UNIQUE KEY `full_add` (`full_add`)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum