I have a mysql table that contains goalscorer information for a list of soccer matches. The table is structured like this:
ID | S1 | S2 | S3 | S4 | S5 | S6 |
---|---|---|---|---|---|---|
1 | 11 | 11 | 18 | 9 | 0 | 0 |
2 | 10 | 3 | 10 | 0 | 0 | 0 |
3 | 10 | 11 | 11 | 18 | 11 | 0 |
4 | 11 | 11 | 18 | 9 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 |
6 | 7 | 10 | 0 | 0 | 0 | 0 |
… | … | … | … | … | … | … |
In this table, ID
corresponds to an individual match ID – where as the numbers under each Sx
column identify individual players and are cross-referenced with another table.
The following query lets me identify instances where player 11 scored more than once
SELECT *
FROM `matchscorer` WHERE (`S1` = '11' AND (`S2` = '11' OR `S3` = '11' OR `S4` = '11' OR `S5` = '11' OR `S6` = '11')) OR (`S2` = '11' AND (`S3` = '11' OR `S4` = '11' OR `S5` = '11' OR `S6` = '11')) OR (`S3` = '11' AND (`S4` = '11' OR `S5` = '11' OR `S6` = '11')) OR (`S4` = '11' AND (`S5` = '11' OR `S6` = '11')) OR (`S5` = '11' AND (`S6` = '11'));
This returns the rows in which 11 appears more than once across the various columns – and I can then manually count those rows to get a total for how many times that happened.
What I wish to achieve, however, is a query that will identify occurrences throughout the table such as this for all players, then (I think) using GROUP BY
to then output a list showing how many times a player scored more than once e.g. Player 11 – 3, Player 10 – 2.
The main blocker I have at the moment is adapting the query from a single player (11) to any player. Nothing I’ve found through research so far seems to focus on this type of query. Should I instead use SELECT DISTINCT
or COUNT DISTINCT
statements to get the desired output.
Is it then possible to isolate the query even further for instances of a player scoring just twice, or thrice, or even four times? Would this require something like count = 3
?
2
Answers
Assuming Sx columns are indexes of goals scored
You can create a new table with the following columns:
match_id | player | goal_index
Then, you can group this table by
match_id
andplayer
columns, and count how many times each player scored.From there you should be able to easily find players who scored in
N
games.If you are using MySQL ≥ 8.0.14, you can
CROSS JOIN
to a lateral derived table to unpivot the data, as suggested by GarethD in the comments:Or, if you are using MySQL < 8.0.14, you can use a
CROSS JOIN
to a derived table and then use the CASE operator to switch columns:For the sample data provided, both of the above queries will output:
As suggested by Sitethief in the comments, you really should consider restructuring your data. If you create a new table with
(match_id, player_id, goal_index)
, you can easily populate it using the inner queries above. You could replace the existingmatchscorer
table with a view so that existing SELECT queries still work. Then you would only need to update parts of your application that write to the table. Easy for me to say with no knowledge of the scope/scale of your app 😉Here’s a db<>fiddle.