开发者

Query to return results based on row_count in associated table?

I have two tables: Match and MatchShots.

A match has many match_shots, and match_shots belong to match.

In my Match table I have an attribute called shot_limit.

I want to return just those matches based on the following conditions:

  • matches shot_limit is not null
  • matches shot_limit = 1 and count of match_shots > 0
  • matches shot_limit = 3 and count of match_开发者_StackOverflowshots > 2


To see how each "Match" quantifies the different classifications, I would add the counts as columns in the result set.

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


How about something like:

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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜