I have 4 tables in a mysql database:
Results
| Result_ID | Season_ID |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 4 | 2 |
| 4 | 2 |
| … | … |
| 37 | 5 |
| 38 | 5 |
Players
Player_ID | Player | Player_Type | Player_Order
1 Fred Defender-Left 1
2 Bert Defender-Left 1
3 Joe Defender-Right 2
4 Harry Midfielder 3
5 Simon Midfielder 3
6 Tate Striker 4
7 Graeme Striker 4
8 Jeff Keeper 5
… … … …
PointTypes
PointType_ID | L1_Value | L2_Value | Point_Value | L1_Order
1 Appearance Keeper 1 1
2 Appearance Outfield 1 1
3 Goal Keeper 4 1
4 Goal Outfield 4 1
5 Assist Keeper 3 1
6 Assist Outfield 3 2
… … … … …
Points
Point_ID | Player_ID | Result_ID | PointType_ID
1 1 1 1
2 2 1 2
3 3 1 2
… … … …
1 1 37 1
2 2 37 2
3 3 37 2
2 4 37 2
3 6 37 2
4 4 37 6
6 6 37 4
1 1 38 1
2 2 38 2
3 3 38 2
2 4 38 2
3 5 38 2
4 2 38 4
6 5 38 6
I'm trying to run a query that produces the following result set:
Player_Order | Player_Type | Player_ID | Player_Name | Sum(Point_Value) | Season_ID
1 Defender-Left 1 Fred 2 5
1 Defender-Left 2 Bert 6 5
2 Defender-Right 3 Joe 2 5
3 Midfielder 4 Harry 5 5
3 Midfielder 5 Simon 4 5
4 Striker 6 Tate 5 5
… … … … … …
The following query provides the all data I want (excluding the Season_ID field).
SELECT Players.Player_Order, Players.Player_Type, Players.Player_ID, Players.Player, SUM(PointTypes.Point_Value) AS Points
FROM Points, Players, PointTypes AS PT
WHERE Players.Player_ID = Points.Player_ID AND Points.PointType_ID = PointType.PointType_ID
GROUP BY Player_ID
ORDER BY Player_Order, Points DESC, Player;
How do I update the query (or even use a different type of query) to filter the data to show the same fields but adding a filter to show only the results where Results.Season_ID = Max(Results.Season_ID).
mysqlyou just wrote it in the question - I have now tagged it. 2) you didn't use table markdown, again I have converted your first lot of data into table markdown, I highly encourage you to convert the rest.JOINsyntax. Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed!WHERE, since aggregation happens after selecting rows. You have to do it inHAVING.