2010-12-09 9 views
0

私はMatchとMatchShotsという2つのテーブルを持っています。関連付けられたテーブルのrow_countに基づいて結果を返すクエリ?

マッチには多くのmatch_shotsがあり、match_shotsはマッチに属します。

私のマッチテーブルには、shot_limitという属性があります。 shot_limitがnullでない

  • 試合
  • 試合は> 0
  • 一致= 3カウントをshot_limit = 1とmatch_shotsの数をshot_limit:

    は、私は、次の条件に基づいて、ちょうどそれらの一致を返すようにしたいですmatch_shotsの> 2

+0

箇条書き=および/または? – btiernay

答えて

0

"Match"がそれぞれの分類をどのように定量化しているかを見るために、結果セットに列としてカウントを追加します。

select 
     m.matchID, 
     {whatever other columns}, 
     count(*) MatchCount 
    from 
     match m, 
     matchShots ms 
    where 
      m.matchID = ms.MatchID 
     and (m.shot_Limit = 1 or m.shot_Limit = 3) 
    group by 
     m.matchID 
    having 
     MatchCount >= m.Shot_Limit 
0

どのようなものについて:

select 
    m.* 
from 
    match m 
    inner join 
    matchshot ms 
    on ms.id = m.ms_id 
where 
    m.shot_limit is not null 
group by 
    m.id 
having 
    (m.shot_limit = 1 and count(*) > 0) or 
    (m.shot_limit = 3 and count(*) > 2)