Archive for October 20th, 2010
Correlated LEFT JOIN in a subquery
Answering questions asked on the site.
Beren asks:
In my database, I store the information about the game matches.
A match can be played between two players (mostly) or, occasionally, between two teams of 2, 3 or 4 players.
I store the matches in a table like this:
Game id player1 player2 type , where
type
isENUM(player, team)
.If
type
isplayer
, the ids of players are stored in the record; if type isteam
, those of teams are stored.Now, the tricky part. For a given game, I need to select two lists of players from both sides, comma separated, be they single players or members of a team.
Are two separate queries required for this?
This of course can be easily done using a single query.
Let's make a sample table and see:
Read the rest of this entry »