MySQL:计算获胜狂欢

时间:2015-03-11 21:13:27

标签: mysql

我有下表:

mysql> describe match_bet_log;
+---------------+---------------------+------+-----+---------+-------+
| Field         | Type                | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------+-------+
| matchId       | bigint(20) unsigned | NO   | PRI | NULL    |       |
| nick          | char(25)            | NO   | PRI | NULL    |       |
| teamId        | tinyint(3) unsigned | NO   | MUL | NULL    |       |
| correct_guess | tinyint(1)          | NO   | MUL | NULL    |       |
| dt            | datetime            | NO   | MUL | NULL    |       |
+---------------+---------------------+------+-----+---------+-------+

我正在尝试为nick计算“获胜狂欢”。获胜的狂欢量将通过以下参数计算:

给定一个缺口,使用dt从最新的correct_guess记录开始,将correct_guess = True的数量返回到第一个correct_guess = False。

任何帮助计算纯粹在MySQL中的“获胜狂欢”都会很棒。我知道如何将所有这些数据都放到python中解析并以这种方式计算获胜的狂欢,但我正在寻找一种可以在MySQL中完全完成的更优雅的方式。

1 个答案:

答案 0 :(得分:0)

尝试此查询并提供反馈:

select t.nic, count(t.correct_guess) from
table_name t
where t.correct_guess=1
and t.dt > (select max(tb.dt) from table_name tb 
          where tb.correct_guess=0 and tb.matchId=t.matchId)
group by t.nic