如何从与比赛表相关的比赛表中得出比赛的获胜者?

发布于 2024-11-27 23:06:13 字数 497 浏览 2 评论 0原文

In a database that stores tournaments I have this relational structure to store matches and their games:

EER Diagram

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?

Here is some sample data

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城空念 2024-12-04 23:06:13

让我们看看我是否正确理解了您的示例数据。

SELECT match_id, round_id, COUNT(*) AS game_count
  FROM games
  GROUP BY match_id, round_id

这将为您提供每场比赛的比赛场数。这场比赛是最好的赛制,所以你必须赢得超过一半的比赛才能赢得比赛。

SELECT match_id, round_id, player_id, COUNT(*) AS win_count
  FROM game_winners
  GROUP BY match_id, round_id

这将为您提供每位玩家在每场比赛中赢得的比赛数。

SELECT match_id, round_id, player_id
  FROM game_winners
  GROUP BY match_id, round_id
  HAVING COUNT(*) * 2 > (
    SELECT COUNT(*)
      FROM games
      WHERE games.match_id = game_winners.match_id
      AND games.round_id = game_winners.round_id
  )

因此,一种选择是通过子查询添加一项检查,以确保玩家赢得了比赛中一半以上的比赛。

SELECT games_won.match_id, games_won.round_id, games_won.player_id
  FROM (
    SELECT match_id, round_id, player_id, COUNT(*) AS win_count
      FROM game_winners
      GROUP BY match_id, round_id
    ) AS games_won
  INNER JOIN (
    SELECT match_id, round_id, COUNT(*) AS game_count
      FROM games
      GROUP BY match_id, round_id
    ) AS all_games
  ON games_won.match_id = game_count.match_id
  AND games_won.round_id = game_count.round_id
  WHERE games_won.win_count * 2 > all_games.game_count

或者,如果您愿意,也可以使用联接来完成此操作。

Let's see if I've understood your sample data correctly.

SELECT match_id, round_id, COUNT(*) AS game_count
  FROM games
  GROUP BY match_id, round_id

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.

SELECT match_id, round_id, player_id, COUNT(*) AS win_count
  FROM game_winners
  GROUP BY match_id, round_id

This gives you the number of games won by each player in each match.

SELECT match_id, round_id, player_id
  FROM game_winners
  GROUP BY match_id, round_id
  HAVING COUNT(*) * 2 > (
    SELECT COUNT(*)
      FROM games
      WHERE games.match_id = game_winners.match_id
      AND games.round_id = game_winners.round_id
  )

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.

SELECT games_won.match_id, games_won.round_id, games_won.player_id
  FROM (
    SELECT match_id, round_id, player_id, COUNT(*) AS win_count
      FROM game_winners
      GROUP BY match_id, round_id
    ) AS games_won
  INNER JOIN (
    SELECT match_id, round_id, COUNT(*) AS game_count
      FROM games
      GROUP BY match_id, round_id
    ) AS all_games
  ON games_won.match_id = game_count.match_id
  AND games_won.round_id = game_count.round_id
  WHERE games_won.win_count * 2 > all_games.game_count

Or if you prefer you can do it using a join.

拥有 2024-12-04 23:06:13

我在这里假设一场比赛的获胜者是赢得比赛最多的玩家。

对一场比赛中的球员进行排名很简单。

SELECT player_id, COUNT(*) AS game_count
  FROM game_winners
  WHERE match_id = ?
  GROUP BY player_id
  ORDER BY COUNT(*) DESC

一次找出多场比赛的获胜者有点棘手。

SELECT match_id, player_id, COUNT(*) AS game_count
  FROM game_winners
  GROUP BY match_id, player_id

我们将上面的内容称为player_wins,

SELECT match_id, MAX(game_count) AS match_winner
  FROM player_wins
  GROUP BY match_id

它为您提供每场比赛的获胜分数。

SELECT match_id, player_id
  FROM player_wins
  INNER JOIN (
    SEELCT match_id, MAX(game_count) AS match_winner
    FROM player_wins
    GROUP BY match_id
  ) AS winning_score
  ON player_wins.match_id = winning_score.match_id
  AND player_wins.game_count = winning_score.match_winner

这将为您提供每场比赛的获胜球员。

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.

SELECT player_id, COUNT(*) AS game_count
  FROM game_winners
  WHERE match_id = ?
  GROUP BY player_id
  ORDER BY COUNT(*) DESC

Finding the winners of more than one match at once is a little tricker.

SELECT match_id, player_id, COUNT(*) AS game_count
  FROM game_winners
  GROUP BY match_id, player_id

Let's call the above player_wins

SELECT match_id, MAX(game_count) AS match_winner
  FROM player_wins
  GROUP BY match_id

This gives you the winning score for each match.

SELECT match_id, player_id
  FROM player_wins
  INNER JOIN (
    SEELCT match_id, MAX(game_count) AS match_winner
    FROM player_wins
    GROUP BY match_id
  ) AS winning_score
  ON player_wins.match_id = winning_score.match_id
  AND player_wins.game_count = winning_score.match_winner

This gives you the winning player for each match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文