title: Improving sql query for selecting amount of games played and amount of games won by player
body: <p>I've got some problem with sql-query I can't solve.</p>
  
  <p>I have 2 tables:</p>
  
  <pre><code>players: id, name
  games: id, name, player1_id, player2_id, winner_id
  </code></pre>
  
  <p>Now I want to get total amount of games and wins for each player.</p>
  
  <p>Currently I'm doing it with sub-selects:</p>
  
  <pre><code>SELECT player.name AS name, 
  (SELECT COUNT(id) FROM replays AS replay WHERE replay.player1_id = player.id OR replay.player2_id = player.id) AS games,
  (SELECT COUNT(id) FROM replays AS replay WHERE replay.winner_id = player.id) AS wins
  FROM players AS player
  </code></pre>
  
  <p>But first of all it is terribly slow.
  And than I am not able to make complicated search queries like search for players with wins more than smth or games less that smth.</p>
  
  <p>Do you have any ideas how can I improve this query?</p>
  
  <p>Thank you in advance.</p>