I am currently pulling all the results from my database where the results are where season = '19'. I would like to pull the last 5 results of each teamname where the season = '19' but cannot work out how to do this.
I think it would look like the following:
Select (each teamname from footballresults and get last 5 games) Teamname, score1, score2 from footballresults where Code = '0101' and Season = '19' and gamedate < CURDATE()
The current code is:
Select Teamname, score1, score2 from footballresults where Code = '0101' and Season = '19' and gamedate < CURDATE()
Currently the script will pull every result for that league in the season but essentially i would like to loop for each team and get the last 5 results for EACH team so
Currently it gets all the games for each team ( select * from DB where season = '19')
I would like to only return the last 5 games for each team so:
Select * from(
Team A Last 5 games
Team B Last 5 games
Team C Last 5 games
Team D Last 5 games
Team E Last 5 games
)
for each teamname loop through and select only last 3 games order by gamedate (The following example is if I wanted the last 3 results per team)-
+--------+---+---+--+
| Team a | 1 | 1 | |
+--------+---+---+--+
| Team a | 4 | 1 | |
+--------+---+---+--+
| Team a | 1 | 3 | |
+--------+---+---+--+
| Team b | 0 | 0 | |
+--------+---+---+--+
| Team b | 1 | 2 | |
+--------+---+---+--+
| Team b | 2 | 0 | |
+--------+---+---+--+
I am using MYSQL version 5.6