根据比赛表中的胜率来获得积分/排名

时间:2015-09-27 17:21:32

标签: mysql sql

我有一张Players表和MatchUps表。 MatchUps表格包含winner_id列和loser_id列。我可以在一个查询中获得基于胜率的排名/排名吗?如果我可以为每个玩家返回这样的东西,那就太好了。

PlayerName - winCount - lossCount - winPercentage

我是一个SQL轻量级,所以我尝试过的东西还没有结束。我基本上只能为每位球员获得胜利数,就是这样。

这是一个示例玩家表:

+----+-------------------------------------+
| id | name                                |
+----+-------------------------------------+
|  1 | Iron Man                            |
|  2 | Gaurdians Of The Galaxy             |
|  3 | Batman Begins                       |
|  4 | X-Men                               |
|  5 | Captain America: The First Avenger  |
|  6 | Superman II                         |
|  7 | Captain America: The Winter Soldier |
|  8 | X2: X-Men United                    |
|  9 | X-Men: First Class                  |
| 10 | Superman: The Movie                 |
| 11 | Batman                              |
| 12 | The Avengers                        |
| 13 | The Incredibles                     |
| 14 | The Dark Knight                     |
+----+-------------------------------------+

以下是一个示例匹配表:

+----+-----------+----------+
| id | winner_id | loser_id |
+----+-----------+----------+
|  1 |         5 |        6 |
|  2 |         2 |        9 |
|  3 |         1 |        5 |
|  4 |         1 |        6 |
|  5 |         4 |       13 |
|  6 |         1 |       13 |
|  7 |         1 |        2 |
|  8 |         1 |        9 |
|  9 |         3 |        8 |
| 10 |         2 |        8 |
| 11 |         1 |        8 |
| 12 |         1 |       12 |
| 13 |         2 |       10 |
| 14 |         1 |       10 |
| 15 |         2 |        4 |
| 16 |         1 |        4 |
| 17 |         2 |       13 |
| 18 |         3 |       11 |
| 19 |         2 |        3 |
| 20 |         1 |        3 |
+----+-----------+----------+

2 个答案:

答案 0 :(得分:1)

这是一种方法:

<强>小提琴: https://github.com/flavienlaurent/discrollview/pull/9

select  p.name as playername,
        x.wincount,
        x.losscount,
        case when x.wincount = 0 then 0
             else x.wincount / x.total_games
             end as winpercentage
from    (   select  player_id,
                    sum(case when outcome = 'W' then 1 else 0 end) as wincount,
                    sum(case when outcome = 'L' then 1 else 0 end) as losscount,
                    count(*) as total_games
            from    (
                        select  winner_id as player_id,
                                'W' as outcome
                        from    matchups
                        union all
                        select  loser_id,
                                'L'
                        from    matchups
                    ) x
            group by player_id
        ) x
   join players p 
     on x.player_id = p.id

答案 1 :(得分:0)

请试试这个:

select id,name,
ifnull(t1.win_count,0) as win_count,
ifnull(t2.loss_count,0) as loss_count,
concat(round((ifnull(t1.win_count,0) / (ifnull(t2.loss_count,0)+ifnull(t1.win_count,0))) * 100,0),' %') as win_percentage
from player as tbl
left join (
select winner_id,count(*) as win_count from matchups group by winner_id
) as t1 on t1.winner_id = tbl.player_id
left join (
select loser_id,count(*) as loss_count from matchups group by loser_id
) as t2 on t2.loser_id = tbl.player_id