I am trying to create a query which will return the most recent 5 entries for each Team
The table is team, reg_no, amount, date
The data is entered in all sorts of dates, so one team may have their most recent 5 entries spanning 2 years, whilst another may have the most recent 5 entries spanning 2 weeks.
I was thinking along the lines of Distinct, but how would I limit the return to 5 entries for each team?
The table Teams holds the team name and data pertaining to each team, I am only interested in the Team Name from that table.
The data1 table holds the reg_no, amount and date
Here's what I tried, but I can't seem to get the DISTINCT statement intergrated.
Any help is appreciated.
Nige.
$query2 = "SELECT Distinct Team FROM Teams";
$row = mysql_fetch_assoc($query2,$db);
$query3 = " CREATE TEMPORARY TABLE tmp1 SELECT * from data1
LEFT JOIN data1 ON Teams.reg_no = data1.reg_no
WHERE Teams.Team='$row["Team"]'
ORDER BY data1.Date DESC
LIMIT 5";
$mysql_result2 = mysql_query ($query3, $db);
The table is team, reg_no, amount, date
The data is entered in all sorts of dates, so one team may have their most recent 5 entries spanning 2 years, whilst another may have the most recent 5 entries spanning 2 weeks.
I was thinking along the lines of Distinct, but how would I limit the return to 5 entries for each team?
The table Teams holds the team name and data pertaining to each team, I am only interested in the Team Name from that table.
The data1 table holds the reg_no, amount and date
Here's what I tried, but I can't seem to get the DISTINCT statement intergrated.
Any help is appreciated.
Nige.
$query2 = "SELECT Distinct Team FROM Teams";
$row = mysql_fetch_assoc($query2,$db);
$query3 = " CREATE TEMPORARY TABLE tmp1 SELECT * from data1
LEFT JOIN data1 ON Teams.reg_no = data1.reg_no
WHERE Teams.Team='$row["Team"]'
ORDER BY data1.Date DESC
LIMIT 5";
$mysql_result2 = mysql_query ($query3, $db);