选择所有组合

时间:2015-05-06 14:31:34

标签: mysql

用单词来解释我试图完成的事情是非常困难的。所以我试着做一个反映我真实项目的例子。我有一群人(Peter,Aaron,Mark,Alicia,Cleo和Mike)互相玩游戏(A,B和C)。我想用我的查询做的是让所有组合对于特定游戏互相对战。 例如:

  1. 彼得打了一场比赛,对阵亚伦,他在3轮比赛中获胜 - >彼得 - 亚伦 - A - 1 - 5 - 1
  2. 迈克与克莱奥队进行了两场C场比赛,并且在3轮和4轮比赛中两次失利 - > 迈克 - 克莱奥 - C - 0 - 3.5 - 2
  3. 桌面游戏示例:

    id   playRound   player   game   win   rounds 
    
    1    1           Peter    A      1     5
    2    1           Aaron    A      0     5
    3    1           Alicia   B      0     3 
    4    1           Mark     B      1     3
    5    1           Mike     C      0     4
    6    1           Cleo     C      1     4
    7    2           Alicia   A      1     5
    8    2           Mark     A      0     5
    9    2           Peter    B      1     3
    10   2           Mark     B      0     3
    11   2           Mike     C      0     3
    12   2           Cleo     C      1     3
    13   3           Alicia   A      0     4
    14   3           Mark     A      1     4
    15   3           Peter    B      0     4
    16   3           Aaron    B      1     4
    17   3           Alicia   C      1     5
    18   3           Cleo     C      0     5
    

    最终结果:

    playerONE   playerTwo   game   avgWinRateOne   avgRounds   Number
    
    Peter       Aaron       A      1               5           1
    Peter       Aaron       B      1               3           1
    Peter       Mark        B      0               4           1
    Alicia      Mark        A      0.5             4.5         2
    Alicia      Mark        B      0               3           1
    Alicia      Cleo        C      1               5           1
    Mike        Cleo        C      0               3.5         2
    

    我正在摆弄这个,但我不知道我在做什么

    SELECT *
    FROM (
    
    SELECT *
    FROM `testtable`
    WHERE `game` = 'A'
    GROUP BY `gameId` , `game` , `player`
    )tmp1, (
    
    SELECT *
    FROM `testtable`
    WHERE `game` = 'A'
    GROUP BY `gameId` , `game` , `player`
    )tmp2
    

1 个答案:

答案 0 :(得分:4)

我想也许你想要这个查询:

select p1.player, p2.player, p1.game, avg(p1.win), avg(p1.rounds), count(*)
  from games p1
    inner join games p2
      on p1.playround = p2.playround
        and p1.game = p2.game
        and p1.player != p2.player
  group by p1.player, p2.player, p1.game;

它是一个相对基本的join,我们将游戏表与自身相关联,以获得我们的游戏配对,我们可以group by这些配对,以便使用聚合函数来计算平均值和比赛次数。

在样本数据上运行它会得到以下结果:

mysql> select p1.player, p2.player, p1.game, avg(p1.win), avg(p1.rounds), count(*)
    ->   from games p1
    ->     inner join games p2
    ->       on p1.playround = p2.playround
    ->         and p1.game = p2.game
    ->         and p1.player != p2.player
    ->   group by p1.player, p2.player, p1.game;
+--------+--------+------+-------------+----------------+----------+
| player | player | game | avg(p1.win) | avg(p1.rounds) | count(*) |
+--------+--------+------+-------------+----------------+----------+
| Aaron  | Peter  | A    |      0.0000 |         5.0000 |        1 |
| Aaron  | Peter  | B    |      1.0000 |         4.0000 |        1 |
| Alicia | Cleo   | C    |      1.0000 |         5.0000 |        1 |
| Alicia | Mark   | A    |      0.5000 |         4.5000 |        2 |
| Alicia | Mark   | B    |      0.0000 |         3.0000 |        1 |
| Cleo   | Alicia | C    |      0.0000 |         5.0000 |        1 |
| Cleo   | Mike   | C    |      1.0000 |         3.5000 |        2 |
| Mark   | Alicia | A    |      0.5000 |         4.5000 |        2 |
| Mark   | Alicia | B    |      1.0000 |         3.0000 |        1 |
| Mark   | Peter  | B    |      0.0000 |         3.0000 |        1 |
| Mike   | Cleo   | C    |      0.0000 |         3.5000 |        2 |
| Peter  | Aaron  | A    |      1.0000 |         5.0000 |        1 |
| Peter  | Aaron  | B    |      0.0000 |         4.0000 |        1 |
| Peter  | Mark   | B    |      1.0000 |         3.0000 |        1 |
+--------+--------+------+-------------+----------------+----------+
14 rows in set (0.00 sec)