1

我有两张桌子scoregame. 每场比赛都有n得分。我想得到n每场比赛的得分。所以我会得到:

round, score, game
==================
1      1      1
2      2      1
3      3      1
1      9      2
2      3      2

小提琴示例:

http://sqlfiddle.com/#!2/56434/1

4

1 回答 1

2
SELECT  RoundNo, Score, ID
FROM
        (
            select  a.ID,
                    b.Score,
                    @sum := if(@nme = a.ID, @sum ,0) + 1 RoundNo,
                    @nme := a.ID
            from    game a
                    INNER JOIN scores b
                      ON a.ID = b.game_ID
                    CROSS JOIN
                    (select @nme := '', @sum := 0) vars
            order   by  a.ID
        ) s
ORDER   BY ID

输出

╔═════════╦═══════╦════╗
║ ROUNDNO ║ SCORE ║ ID ║
╠═════════╬═══════╬════╣
║       1 ║    14 ║  1 ║
║       2 ║    10 ║  1 ║
║       1 ║    19 ║  2 ║
║       2 ║    20 ║  2 ║
║       3 ║    25 ║  2 ║
╚═════════╩═══════╩════╝
于 2013-04-02T05:09:17.650 回答