规范化超出 MySQL 知识范围?

发布于 2024-08-03 02:09:14 字数 1250 浏览 3 评论 0原文

我认为我已经规范了我的数据库,超出了我的 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 技术交流群。

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

发布评论

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

评论(1

半仙 2024-08-10 02:09:14

我没有现成的 MySQL 安装,但您想要的可能是 EXISTS 子句,尝试一下,如果它满足您的需要,请回复:

select max(break_score) maxBreak, avg(break_score) avgBreak, count(break_score) breaks
from breaks
join matches on match_id=break_match
where exists(select 1
from players
join team_members on player=player_id and team=3
where break_year = year
and break_player = player_id
and (match_hometeam=3 or match_awayteam=3))

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:

select max(break_score) maxBreak, avg(break_score) avgBreak, count(break_score) breaks
from breaks
join matches on match_id=break_match
where exists(select 1
from players
join team_members on player=player_id and team=3
where break_year = year
and break_player = player_id
and (match_hometeam=3 or match_awayteam=3))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文