This questions is similar to what I need but the answer seems rather specific to the question rather than a general answer. One to many relationship query result in a single row and dynamic multiple columns
I have a table of games (we will call this table matches)
gameID Home Team Away Team DateTime
----- ----------- ---------- ----------
1 Barcelona Real Madrid 12-08-21 10:00:00
2 Valencia Sevilla 12-08-21 10:00:00
Each game has potentially several attributes, half time score, full Tim score, extra time score, penalty score, attendance etc.
This is stored in a one to many table which looks like (we will call this match attributes)
id game id attribute id attribute value
----- -------- ----------- -----------------
1 1 1 3
2 1 2 3
3 1 5 32123
4 2 1 4
5 2 2 0
Where attribute id is a foreign key for this table (we will call this attributes)
attribute id attribute name
---------- ---------------
1 Home Team Extra Time Score
2 Away Team Extra Time Score
5 Game Attendance
8 Home Team Penalty Score
5 Away Team Penalty Score
At the moment when I want to show a match to a user I query the matches table and separately query the match attributes table, ideally however I could make one join that merges the several results from the match attributes table into columns of the matches table. Here are two examples.
Querying 1 game (Where gameId = 1)
gameID Home Team Away Team DateTime Home Team Extra Time Score Away Team Extra Time Score Attendance
----- ----------- ---------- ---------- ---------------------------- ---------------------------- ----------
1 Barcelona Real Madrid 12-08-21 10:00:00 3 3 32123
Querying multiple games (game id = 1 or 2)
gameID Home Team Away Team DateTime Home Team Extra Time Score Away Team Extra Time Score Attendance
----- ----------- ---------- ---------- ---------------------------- ---------------------------- ----------
1 Barcelona Real Madrid 12-08-21 10:00:00 3 3 32123
2 Valencia Sevilla 12-08-21 10:00:00 4 0 null
How would this be possible, I don't even know what functionality I could use to do this, as I don't think joins would work with multiple rows like this