Skip to content

Instantly share code, notes, and snippets.

@outofjungle
Created February 12, 2016 07:45
Show Gist options
  • Select an option

  • Save outofjungle/1dc4b1e8076cd8078b6e to your computer and use it in GitHub Desktop.

Select an option

Save outofjungle/1dc4b1e8076cd8078b6e to your computer and use it in GitHub Desktop.
games sql
CREATE TABLE wins (
win_id INT NOT NULL,
game_id INT NOT NULL,
winner_id INT NOT NULL,
PRIMARY KEY (win_id)
);
CREATE TABLE games (
game_id INT NOT NULL,
player_id INT NOT NULL,
color ENUM('white', 'black') NOT NULL,
PRIMARY KEY (game_id)
);
# count of all the wins for a player
SELECT COUNT(wins.win_id) FROM wins JOIN games ON wins.winner_id = games.player_id AND games.player_id = $PLAYER
# count of all the games against a player
SELECT game_id, player_id, winner_id, color FROM games JOIN wins ON wins.game_id = games.games_id AND games.player_id = $PLAYER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment