Skip to content

How do I select all matches in a database that have two particular players?

The situation:

Matches have 10 players, 5 on each team. There are 100 players total and each match can have any player in any of the ten spots. I need to extract the following given two players:

  1. How many matches were player A and player B allies?
  2. How many of these did they win?
  3. How many matches were player A and player B opponents?
  4. How many of these did player A win?

My current method (too slow and possibly doesn’t work):

I’ve made three tables, “players”, “matches” and “matches_players” which maps the matches to the players that played in them. The column “winning_team” in matches represents which team won (0 = team A, 1 = team B) and column “position” in the matches_players table represents what spot the player was in the lineup. 0-4 is on team A, 5-9 is on team B.


players



matches



matches_players


Here’s the statement I’m using for finding match and win counts of heroes on the same team (after over 12 hours of surfing and trying to understand SQL):

It seems to work, but it’s incredibly slow (90+ secs on 250,000 matches) and ideally I’d like to have opposing team results in the same query (which should be faster than two separate queries, no?) Can the query be made faster/better? Is there a database design flaw?

I really appreciate any help guys. Advice in any area is welcome. Thanks.

Advertisement

Answer

I think this should do it or at least give you a hint

User contributions licensed under: CC BY-SA
1 People found this is helpful