PDA

View Full Version : Do Some Calculations on Query Results

transfield
03-04-2007, 05:05 PM
Hello,
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.

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

if(isset(\$_POST['Submit'])){
\$emails=explode("\n", str_replace("\r", "", \$_POST['femail']));
\$email_r=array();
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;
\$result1=mysql_query(\$query1);
}

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

<html>
<head>
<title>Test</title>
</head>
<body>
<form name="ftest" action="<?= \$_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="femail"></textarea><br />
<input type="submit" name="Submit" value="Send" />
</form>
<table width=700 border=1 height="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<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>
</tr>
</table>
</body>
</html>
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',
PRIMARY KEY (`id`),
UNIQUE KEY `full_add` (`full_add`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1