每个玩家每个事件MySQL赢得Streak

时间:2016-03-22 10:50:48

标签: mysql

我尝试过“连胜”搜索标准的不同解决方案,但在我的情况下我无法看到它们。

我有以下表格:

BetHistory:

+-------+-------+-----------+
|   id  | betid | betstatus |
+-------+-------+-----------+
| 63281 | 63280 | lose      |
| 63280 | 63279 | won       |
| 63279 | 63278 | lose      |
| 63278 | 63277 | lose      |
| 63276 | 63275 | won       |
| 63275 | 63274 | won       |
| 63271 | 63270 | won       |
| 63270 | 63269 | won       |
| 63269 | 63268 | won       |
| 63268 | 63267 | won       |
| 63267 | 63266 | lose      |
| 63266 | 63265 | won       |
| 63264 | 63263 | won       |
| 63262 | 63261 | won       |
| 63261 | 63260 | lose      |
| 63259 | 63258 | lose      |
| 63258 | 63257 | won       |
| 63257 | 63256 | lose      |
| 63256 | 63255 | lose      |
| 63254 | 63253 | won       |
+-------+-------+-----------+

投注:

+-------+--------+----------+
| betid | gameid | username |
+-------+--------+----------+
| 63280 |   7506 | player1  |
| 63279 |   7506 | player2  |
| 63278 |   7506 | player5  |
| 63277 |   7506 | player2  |
| 63275 |   7506 | player12 |
| 63274 |   7506 | player12 |
| 63270 |   7506 | player5  |
| 63269 |   7505 | player2  |
| 63268 |   7505 | player12 |
| 63267 |   7505 | player2  |
| 63266 |   7505 | player5  |
| 63265 |   7505 | player1  |
| 63263 |   7505 | player2  |
| 63261 |   7504 | player2  |
| 63260 |   7504 | player5  |
| 63258 |   7504 | player5  |
| 63257 |   7504 | player2  |
| 63256 |   7504 | player2  |
| 63255 |   7503 | player2  |
| 63253 |   7503 | player1  |
+-------+--------+----------+

EventGames: - 另一个存储游戏和事件之间关系的表格,可以根据需要轻松集成。

我需要帮助制定MySQL声明,以确定至少有五(5)场胜利的球员。

提前非常感谢。

修改:下表可能更容易:

+-------+-----------+--------+----------+------+
| id    | betstatus | gameid | username | team |
+-------+-----------+--------+----------+------+
| 63281 | lose      |   7506 | player12 | C    |
| 63280 | won       |   7506 | player7  | A    |
| 63279 | lose      |   7506 | player3  | B    |
| 63278 | lose      |   7506 | player10 | B    |
| 63276 | won       |   7506 | player1  | A    |
| 63275 | won       |   7506 | player1  | A    |
| 63271 | won       |   7506 | player3  | A    |
| 63270 | won       |   7505 | player7  | B    |
| 63269 | won       |   7505 | player1  | B    |
| 63268 | won       |   7505 | player10 | B    |
| 63267 | lose      |   7505 | player3  | A    |
| 63266 | won       |   7505 | player12 | B    |
| 63264 | won       |   7505 | player10 | B    |
| 63262 | won       |   7504 | player10 | B    |
| 63261 | lose      |   7504 | player3  | A    |
| 63259 | lose      |   7504 | player3  | A    |
| 63258 | won       |   7504 | player10 | B    |
| 63257 | lose      |   7504 | player7  | A    |
| 63256 | lose      |   7503 | player7  | A    |
| 63254 | won       |   7503 | player12 | B    |
+-------+-----------+--------+----------+------+

3 个答案:

答案 0 :(得分:1)

您可以使用变量来获得所需的结果:

SELECT username, MAX(streak) AS streak
FROM (
  SELECT username, betstatus, id,
         @streak := IF(betstatus = 'lose', IF(@uid := username, 0, 0),
                      IF(@uid = username, @streak + 1,
                         IF(@uid := username, 1, 1))) AS streak

  FROM (
    SELECT b.username, bh.betstatus, bh.id
    FROM Bets AS b
    INNER JOIN BetHistory AS bh ON b.betid = bh.betid) AS t 
  CROSS JOIN (SELECT @streak := 0, @uid := '') AS vars
  ORDER BY username, id) AS s
WHERE streak >= 5  
GROUP BY username 
ORDER BY username 

注意:我使用了原始表结构。

注意2 :查询使用嵌套 IF表达式来正确初始化和访问变量。

Demo here

答案 1 :(得分:1)

此sql适用于来自编辑的名为BetHistory的联合表。使用的想法来自@Matt的评论。

select Distinct(t.username)
from
    (select bh1.username, bh1.id, count(bh2.id) as streakGroup 
    from BetHistory bh1
    left join BetHistory bh2
    on bh1.id < bh2.id and bh1.username = bh2.username and bh1.betstatus <> bh2.betstatus
    where bh1.betstatus = 'won'
    group by bh1.id, bh1.username) as t
group by t.streakGroup, t.username
having Count(t.id) >= 5

<强> Demo here 即可。 teamgameid列被省略,因为它们未被使用。

答案 2 :(得分:0)

我尝试使用已发布的答案,我感谢大家提出的好主意。

复杂,因为它是检查条纹,我选择只添加一个听众,每当游戏宣布胜利者,赢得双方的连胜增加,输家将从0开始。

然后我可以简单地用条纹条件查询表。

非常感谢,