我有一个像这样的 MySQL 数据库:
ID | Twin | Tloss
0 | 300 | 250 #first entry
1 | 301 | 250 #win; score 1 - 0
2 | 302 | 250 #win; score 2 - 0
3 | 302 | 251 #lose: score 2 - 1
4 | 303 | 251 #win; score 3 - 1
5 | 304 | 251 #end of match1 : Win 4 - 1
6 | 304 | 252 #lose; score 0 - 1
7 | 304 | 253 #lose; score 0 - 2
8 | 304 | 254 #lose; score 0 - 3
9 | 304 | 255 #end of match2 : Lose 0 - 4
10 | 304 | 256 #lose; score 0 - 1
11 | 305 | 253 #win; score 1 - 1
12 | 306 | 254 #win; score 2 - 1
13 | 306 | 255 #lose; score 2 - 2
14 | 307 | 255 #win; score 3 - 2
15 | 307 | 256 #end of match3 : Draw 3 - 3
....
我要选择匹配号“n”对应的所有ID, 考虑到一场比赛只要赢4次或输4次就结束,则有可能平局,因为每场比赛的最大轮数为6。
自 1 个月以来,我经常使用 SQL,但我对此真的很迷茫。
有人可以帮助我吗?
提前谢谢您,
最佳答案
我假设上面的某些数据已损坏(记录 11-15,字段丢失;记录 5 应该是 Win 4 - 1)。我也不知道数字 300 和 250 是什么以及它们在表中如何变化。有了这些假设,这个未经测试的 SQL 可能就是您想要的:
(根据 GordonLinoff 反馈进行修改)
SELECT @matchno := @matchno + 1 AS matchno
FROM (SELECT @matchno := 0) mn,
(SELECT ID, Twin, Tloss,
IF((Twin - @twin) = 4
OR (Tloss - @tloss) = 4
OR ((Twin - @twin) = 3 AND (Tloss - @tloss) = 3),
@twin := Twin AND @tloss := Tloss,
0)
FROM matches, (SELECT @twin := 300, @tloss := 250) AS base
WHERE (Twin - @twin) = 4
OR (Tloss - @tloss) = 4
OR ((Twin - @twin) = 3 AND (Tloss - @tloss) = 3)
ORDER BY ID
) endmatches
关于mysql - 根据行增量选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723287/