Hi All,
I'll try and explain as best as possible what I'm trying to do and hopefully someone can tell me the best way to do it!
I'm basically trying to rank by 2 columns in mysql/php, by this i mean i have a column called `Turnover` and one of `Cash` I would like to display each column in a table with its ranking in the column to the right.
So far i have:
Now obviously this works very well and shows me the data ordered by Turnover with rankA in the column to the right, but what i really want to add next is to have another column with the value of the data ordered by Cash (rankB). But i of course want to keep rankA as well.
I'm not new to mysql but i'm fully aware that i don't know how to use it to its greatest potential. The only solution that i have at the moment is to have a whole seperate php function which computes rank for Turnover, and stores it in the table, then computes rank for Cash and stores that in the table too. Now with the amount of data i have this is really not an option, plus the data is frequently updated so the function would have to be rerun every time.
I'm thinking there must be a way to run the query ordered by Turnover and then somehow carry rankA through for when i run it again ordered by Cash, setting RankB; then display the whole lot, but i simply don't know how.
Any suggestions would be much appreciated.
Thanks
Andy
I'll try and explain as best as possible what I'm trying to do and hopefully someone can tell me the best way to do it!
I'm basically trying to rank by 2 columns in mysql/php, by this i mean i have a column called `Turnover` and one of `Cash` I would like to display each column in a table with its ranking in the column to the right.
So far i have:
Code:
include ("database.php");
global $conn;
mysql_query("set @rankA=0;");
$sql = "
SELECT `name`, `id`, `year`, `Turnover`, `Cash`, @rankA:=@rankA+1 AS rankA
FROM stocks
LEFT JOIN data ON `id` = `company_id`
WHERE `year` = '2007' AND
`Turnover` IS NOT NULL AND
`Turnover` != 0
ORDER BY Turnover DESC
;";
$result = mysql_query($sql);
echo mysql_num_rows($result)."<br />";
echo $sql."<br />";
echo "<table width=200 border=1>\n";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>\n";
echo "\t<td>".$row['id']."</td>\n";
echo "\t<td>".$row['name']."</td>\n";
echo "\t<td>".$row['year']."</td>\n";
echo "\t<td>".$row['Turnover']."</td>\n";
echo "\t<td>".$row['rankA']."</td>\n";
echo "\t<td>".$row['Cash']."</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
mysql_close($link);
I'm not new to mysql but i'm fully aware that i don't know how to use it to its greatest potential. The only solution that i have at the moment is to have a whole seperate php function which computes rank for Turnover, and stores it in the table, then computes rank for Cash and stores that in the table too. Now with the amount of data i have this is really not an option, plus the data is frequently updated so the function would have to be rerun every time.
I'm thinking there must be a way to run the query ordered by Turnover and then somehow carry rankA through for when i run it again ordered by Cash, setting RankB; then display the whole lot, but i simply don't know how.
Any suggestions would be much appreciated.
Thanks
Andy
Comment