Re: Saving score of 3 players into a table - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: Saving score of 3 players into a table
Date
Msg-id 2F862AA7-598E-4418-8CB8-7BDF8150BF1F@seespotcode.net
Whole thread Raw
In response to Re: Saving score of 3 players into a table  (Alexander Farber <alexander.farber@gmail.com>)
Responses Re: Saving score of 3 players into a table  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
On Oct 26, 2011, at 16:15, Alexander Farber wrote:

> Hello again,
>
> still I can't figure out how to perform a join
> to fetch all games where a player has participated -
> I have a table containing all games played:
>
> # select * from pref_games limit 5;
> gid | rounds |          finished
> -----+--------+----------------------------
>   1 |     10 | 2011-10-26 14:10:35.46725
>   2 |     12 | 2011-10-26 14:34:13.440868
>   3 |     12 | 2011-10-26 14:34:39.279883
>   4 |     14 | 2011-10-26 14:35:25.895376
>   5 |     14 | 2011-10-26 14:36:56.765978
>
> And I have a table with scores of each of 3 players:
>
> # select * from pref_scores where gid=3;
>          id           | gid | money | quit
> -----------------------+-----+-------+------
> OK515337846127        |   3 |   -37 | f
> OK40798070412         |   3 |   -75 | f
> MR2871175175044094219 |   3 |   112 | f
>
> (Which means 3 players have played game #3
> and 1 has won 112, while 2 have lost 37 + 75)
>
> My problem is: I'd like to list all games played
> by 1 player, with all participants and scores listed.

Get games for a particular user:

SELECT g.gid, g.rounds, g.finished
  FROM pref_games g
  JOIN pref_scores u USING (gid)
  WHERE u.id = :id;

Now, add the participants for those games

SELECT g.gid, g.rounds, g.finished,
       p.id, p.money, p.quit
  FROM pref_games g
  JOIN pref_scores u USING (gid)
  JOIN pref_scores p USING (gid)
  WHERE u.id = :id;

Michael Glaesemann
grzm seespotcode net


pgsql-general by date:

Previous
From: "Darin Perusich"
Date:
Subject: specifying multiple ldapserver in pg_hba.conf
Next
From: Gavin Flower
Date:
Subject: Re: All and ANY