Here's one for MySql, I'm having trouble figuring out...
Premiss: I want folks to donate money for specific weight class. NCAA wrestling has 10 weight classes. Of all records (rows) I only want to output the top 3 donors (ordered by Amount desc) for each WeightClass.
(I've got it working as long as there's only 3 rows for each weight class in the table.)
table = FightLadder
columns = ID, Name, State, WeightClass, Amount
You can view current output at http://www.saveoregonwrestling.org/fightLadder
Here's my php code to extract the data out of my table -- then you see my code to store the data in vars so I can display them in html, (This is only for the first of 10 weight classes).
[code]
$query="SELECT c.PublishName, c.State, c.Total, c.WeightClass, d.ranknum
FROM FightLadder AS c
INNER JOIN (
SELECT a.ID, COUNT() AS ranknum
FROM FightLadder AS a
INNER JOIN FightLadder AS b ON (a.WeightClass = b.WeightClass) AND (a.Total <= b.Total)
GROUP BY a.ID
HAVING COUNT() <= 3
) AS d ON (c.ID = d.ID)
ORDER BY c.WeightClass, c.Total DESC, d.ranknum";
$result = mysql_query($query) or die("Failed Query of " . $query); //do the query
$champ1a = @mysql_result($result, 0,0);
$champ2a = @mysql_result($result, 0,1);
$champ3a = @mysql_result($result, 0,2);
$runner1a = @mysql_result($result, 1,0);
$runner2a = @mysql_result($result, 1,1);
$runner3a = @mysql_result($result, 1,2);
$consol1a = @mysql_result($result, 2,0);
$consol2a = @mysql_result($result, 2,1);
$consol3a = @mysql_result($result, 2,2);
[/pre]
I do the above vars for A-J. Is there a smarter more efficient way to extract this data? Hopefully, someone can help me out here. BTW - I resorted to using the @ in front of the mysql_result() function because I get "WARNING mysql_result() [something about pointer not able to find next row]" as soon as there's more or less than 30 rows in my table... And the @ symbol eliminates these nagging messages.