规范化超出 MySQL 知识范围?
我认为我已经规范了我的数据库,超出了我的 SQL 知识范围。 :) 这是我在斯诺克联赛网络应用程序中遇到的一个查询。它计算一支球队所有赛季的高突破的总体统计数据。在示例中,团队 ID 3。
select max(break_score) maxBreak,avg(break_score) avgBreak, count(break_score) breaks from breaks join matches on match_id=break_match where break_player in ( select distinct player_id from players join team_members on player=player_id and team=3) and (match_hometeam=3 or match_awayteam=3)
表:
- 匹配:
match_id int(11) auto_increment match_hometeam int(11) match_awayteam int(11) match_date date match_void tinyint(4) match_status tinyint(4) match_homescore tinyint(4) match_awayscore tinyint(4) match_league int(11)
- 休息:
break_id int(11) auto_increment break_match int(11) foreign key to match_id break_player int(11) break_score tinyint(4) break_clearance tinyint(4) break_year int(11)
- team_members:多对多表。
team int(11) foreign key to teams table player int(11) foreign key to players table year int(11) the year that the player played for the team
除了一个问题之外,上面的查询几乎按预期工作。如果一名球员为多支球队效力,那么他在所有球队中的突破都会包含在这些统计数据中。
如果 Breaks 表有一个额外的字段“break_team”,则查询将很简单。所以我的问题有两个,任何人都可以协助正确的查询吗?或者我应该稍微减少标准化以帮助处理这些统计数据?什么时候是去常态化的时候?
I think I've normalised my database beyond my SQL knowledge. :)
Here's a query I'm struggling with from my Snooker league web app. It calculates overall stats for high breaks for a team from all seasons. In the example team id 3.
select max(break_score) maxBreak,avg(break_score) avgBreak, count(break_score) breaks from breaks join matches on match_id=break_match where break_player in ( select distinct player_id from players join team_members on player=player_id and team=3) and (match_hometeam=3 or match_awayteam=3)
Tables:
- matches :
match_id int(11) auto_increment match_hometeam int(11) match_awayteam int(11) match_date date match_void tinyint(4) match_status tinyint(4) match_homescore tinyint(4) match_awayscore tinyint(4) match_league int(11)
- breaks :
break_id int(11) auto_increment break_match int(11) foreign key to match_id break_player int(11) break_score tinyint(4) break_clearance tinyint(4) break_year int(11)
- team_members : many to many table.
team int(11) foreign key to teams table player int(11) foreign key to players table year int(11) the year that the player played for the team
The query above works almost as intended apart from one problem. If a player has played for more than one team then Breaks he has for all teams get included in these stats.
If the Breaks table had an extra field 'break_team' the query would be trivial. So my question is two fold, can anyone assist with the right query or should I reduce the normalisation a bit to help with these stats? When is it time to de-normalise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有现成的 MySQL 安装,但您想要的可能是 EXISTS 子句,尝试一下,如果它满足您的需要,请回复:
I don't have a readily available MySQL install, but what you're after is probably the EXISTS clause, try this and reply back if it takes care of what you need or not: