如何从与比赛表相关的比赛表中得出比赛的获胜者?
在存储锦标赛的数据库中,我使用以下关系结构来存储比赛及其比赛:
EER 图
我希望 Fred 作为 match_ID 1 的获胜者返回,因为他赢了2/3 游戏。我希望 match_ID 2 的获胜者为空,因为两个玩家都没有赢得获胜所需的游戏次数 (2/3)。
In a database that stores tournaments I have this relational structure to store matches and their games:
When a match is created, a match row and multiple game rows are inserted corresponding to the number of games in the match. Match_left and match_right players rows are also inserted then to relate the two players in a match to the match row. When a player is recorded as having won a game, a row related to the corresponding game is inserted into the game_winners table. Is there a way to write a query to derive who won matches from won games? Perhaps logically this should be a view so that I can join match table rows to it at any time easily?
I would expect Fred to be returned as the winner of match_ID 1 because he won 2/3 games. I would expect the winner of match_ID 2 to be null because neither player has won the necessary number of games to win (2/3).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们看看我是否正确理解了您的示例数据。
这将为您提供每场比赛的比赛场数。这场比赛是最好的赛制,所以你必须赢得超过一半的比赛才能赢得比赛。
这将为您提供每位玩家在每场比赛中赢得的比赛数。
因此,一种选择是通过子查询添加一项检查,以确保玩家赢得了比赛中一半以上的比赛。
或者,如果您愿意,也可以使用联接来完成此操作。
Let's see if I've understood your sample data correctly.
This gives you the number of games in each match. The match is a best of format, so you have to win more than half of the number of games to win the match.
This gives you the number of games won by each player in each match.
So one option is to add a check that the player has won more than half the number of games in the match via a subquery.
Or if you prefer you can do it using a join.
我在这里假设一场比赛的获胜者是赢得比赛最多的玩家。
对一场比赛中的球员进行排名很简单。
一次找出多场比赛的获胜者有点棘手。
我们将上面的内容称为player_wins,
它为您提供每场比赛的获胜分数。
这将为您提供每场比赛的获胜球员。
I'm assuming here that the winner of a match is the player who's won the most games.
Ranking the players within a match is straightforward.
Finding the winners of more than one match at once is a little tricker.
Let's call the above player_wins
This gives you the winning score for each match.
This gives you the winning player for each match.