2010-12-09 53 views
0

J'ai deux tables: Match et MatchShots.La requête renvoie les résultats basés sur row_count dans la table associée?

Une correspondance a plusieurs match_shots, et match_shots appartiennent à match.

Dans ma table de match, j'ai un attribut appelé shot_limit.

Je veux revenir seulement ces matches en fonction des conditions suivantes:

  • matchs shot_limit n'est pas nul
  • matchs shot_limit = 1 et le nombre de match_shots> 0
  • matchs shot_limit = 3 et le nombre de match_shots> 2
+0

Puces = et/ou? – btiernay

Répondre

0

Pour voir comment chaque "Match" quantifie les différentes classifications, j'ajouterais les comptes comme des colonnes dans l'ensemble de résultats.

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

que diriez-vous quelque chose comme:

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)