gpt4 book ai didi

mysql - 如果我在一个表中有一组相关的行,我如何用组中的索引来装饰它们?

转载 作者:行者123 更新时间:2023-11-29 06:50:29 24 4
gpt4 key购买 nike

我有两个表scoregame。每个游戏都有 n 分数。我想为游戏的每个分数获取 n。所以我会得到:

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

示例 fiddle :

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

最佳答案

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 ║
╚═════════╩═══════╩════╝

关于mysql - 如果我在一个表中有一组相关的行,我如何用组中的索引来装饰它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com