2009-08-22 6 views
0

SQLの知識を超えてデータベースを正規化したと思います。 :) 私は私のスヌーカーリーグのWebアプリから苦労しているクエリです。それは、全シーズンからチームのハイブレイクの総合的な統計を計算します。例えば、チームのID 3.MySQLの知識を超えて標準化されていますか?

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'がある場合、クエリは簡単です。ですから、私の質問は2倍で、誰かが適切なクエリを手助けすることができますか、これらの統計を助けるために正規化を少し減らすべきですか?正規化を解除するのはいつですか?

答えて

2

私は、容易に入手可能で、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)) 
+0

ルックスあなたがそれを修正したように。あなたのクエリは最後の列で345を収集します。これは、別々の季節合計の合計です。私のクエリは347となり、別のシーズンとチームから数回の休憩を取った。どうもありがとう。 – PaulBM

+0

問題はありません。うれしいことに、うまくいきました...将来の不具合が返ってきたら、私は確認します。 –

関連する問題