Hi there,
I'm wanting to find the ranking / number of a row. I'm not sure if I'm explaining this well so I'll try.
I have the query
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo $row['total'] . '<br />';
}
Now I want to go through and give each a ranking based on the biggest 'total' being number '1'.
So I can do that with php by doing some counting:
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
$rank = 1;
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo 'rank: ' . $rank . ', ' . $row['total'] . '<br />';
$rank = $rank + 1;
}
This is good and working. But what I'm wanting to do, is be able to determine the ranking of a row without php so I can do an sql query based on say an affiliate ID from the sales table.
So for example I have 100 rows of sales data with an affiliate ID linked to each row, how would I go about simply getting the ranking based on the affiliate with the biggest total?