samedi 19 décembre 2015

Seeing if keys in two different tables match when linked by a foreign key

I have 3 tables, match, players, and match_player - the third allowing a many-to-many relationship as each player has many matches, and a match consists of many players.

CREATE TABLE IF NOT EXISTS match
             (id integer primary key,
             date text,
             winning_side integer)

CREATE TABLE IF NOT EXISTS player
            (id integer primary key,
            name text)

CREATE TABLE IF NOT EXISTS match_player
            (id integer primary key autoincrement,
            match_id integer,
            player_id integer,
            side integer,
            foreign key(match_id) references match(id),
            foreign key(player_id) references player(id))

The side can be 1 or 0.

I want to see what % of games a player wins.

I figure a good way of doing this is a SELECT statement on the match_player and then counting the number of entries where side == winning_side. But of course winning_side is in a separate table.

I've been googling on how to easily do this, but can't fathom it.

Aucun commentaire:

Enregistrer un commentaire