我有两张桌子score
和game
. 每场比赛都有n
得分。我想得到n
每场比赛的得分。所以我会得到:
round, score, game
==================
1 1 1
2 2 1
3 3 1
1 9 2
2 3 2
小提琴示例:
我有两张桌子score
和game
. 每场比赛都有n
得分。我想得到n
每场比赛的得分。所以我会得到:
round, score, game
==================
1 1 1
2 2 1
3 3 1
1 9 2
2 3 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 ║
╚═════════╩═══════╩════╝