在 MySQL 中存储锦标赛括号的关系方式是什么?
现在我有一个比赛表,其中每场比赛都有 2 个参赛者和一个获胜者的参考,但没有办法存储回合的顺序。
Right now I have a table for matches where each match has a reference to 2 competing participants and a winner, but there isn't a way to store the order of the rounds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有什么阻止您创建名为“round_order”的字段吗?你知道顺序吗?
Is there anything stopping you from creating a field named "round_order"? Do you know the order?
简单地在匹配表中存储一个“整数”字段似乎就足够了。您可能想以相反的顺序分配轮数,即:1 代表决赛,2 代表半决赛,3 代表四分之一决赛,依此类推,而不是 1 代表第一轮,2 代表第二轮,等等。
这还有一个额外的优点,您可以立即从整数中获取信息。例如,查询所有半决赛将非常容易
... WHERE round_number = 2
,并且将独立于锦标赛轮次的总部门。Simply storing a "round number" field in the matches table seems to be sufficient. You may want to assign the round numbers in reverse order, ie: 1 for the final, 2 for the semi-finals, 3 for the quarter-finals, and so on, instead of 1 for the first round, 2 for the second, etc.
This has the added advantage where you can immediately derive information from the round number. For example doing a query for all the semi-finals will be very easy
... WHERE round_number = 2
, and will be independent from the total dept of the tournament rounds.事实上,我不确定我是否同意。
如果“比赛”表中有一个“回合”字段,则可能会出现完整性问题,因为每轮的表中都会有一行(这实际上没有意义,因为这是“比赛” “表,因此每场比赛应该只有一行)
如果您这样做,每一行中可能有一组不同的参与者,这可能不是您的意图,并表明您应该将“round”键入到不同的参与者桌子。引入的其他完整性问题是“获胜者字段”,例如,如果您在比赛表中列出该轮的获胜者,您如何知道谁赢得了比赛?如果您只是将比赛的获胜者放在所有行中,您就会丢失有关谁赢得了该轮比赛的信息。
要进行适当的标准化,请创建一个“MatchRound”表。
为您的 Match 表指定一个主键(例如,将其称为 Match_Id)
然后你的圆桌会议可以有:
Round_Id(自动编号或类似的唯一主键)、Match_Id(匹配表的外键)以及可能的 RoundOrder 和 RoundType(Final、Semi 等)。您可能也可以将获胜者和失败者添加到此表中。
虽然将 round_number 字段用于半决赛/四分之一决赛等双重用途是可行的,但只有在比赛之间的拓扑结构一致时才有效,例如总是有四分之一决赛?例如,有些运动会累积积分,然后只进行决赛。因此,我只创建 RoundType 文本字段。
Actually, I'm not sure I agree.
If you have a "round" field in the "match" table, you introduce the possibility of integrity problems, as you end up with a row in your table for each round (which doesn't really make sense as this is the "match" table, so there should only be one row per match)
If you do that, each row could potentially have a different set of participants in it, which is likely not your intention and indicates that you should key "round" out to a different table. Other integrity problems introduced would be the "Winner field" e.g. if you list the winner of the round in the match tables, how do you know who won the match? If you just put the winner of the match across all rows, you lose the information about who won the round.
To be appropriately normalised, create, say a "MatchRound" table.
Give your Match table a primary key (call it Match_Id for example)
and then your Round table could have:
Round_Id (autonumber or similar unique primary key), Match_Id (which is a foreign key to the match table) and perhaps RoundOrder and RoundType (Final, Semi, etc). You're probably also okay adding Winner and Loser to this table.
While dual-purposing the round_number field for semis/quarters etc would work, it only works if the topology is consistent across matches, e.g. there are always quarter finals? Some sports accumulate points for example, and then have only a final. For this reason, I'd just create RoundType text field.
分组赛是否是种子队,就像网球比赛(或疯狂三月锦标赛)一样,如果是这样,您是否需要回答诸如“费德勒和纳达尔将在哪一轮比赛(如果有的话)?”之类的问题?然后,您需要将括号存储为树在数据库中。
Is the bracket seeded, like in Tennis (or the March Madness tournament), and if so, will you need to answer questions like "In what round, if at all, will Federer and Nadal play each other?" Then you'd need to store the bracket as a tree in the database.