mysql - Exclude records from one table based on count in joined table -
i have 2 tables: coupons , responses.
the coupons table includes fields such as:
- vendorid [key]
- couponid [key]
- coupondescription
the responses table includes fields such as:
- respondentid [key]
- couponid [key]
- (vendorid not repeated in table.)
- accepted [true/false]
- rejected [true/false]
- rating [1 5]
when accepts coupon, rate in responses table. likewise, when rejects coupon, rejection appears in responses table.
when fetching list of available coupons coupons table, i'd exclude vendors received 2 or more rejections respondent. instance if respondentid 3 rejected 2 or more coupons vendor 47, coupons vendor 47 no longer shown respondentid 3.
two things make difficult sql novice myself.
the first how write subquery counts rejections from particular vendor , by particular respondent.
the second how join-back (so speak) responses table coupons table sub-query of responses table bases result on respondentid (which in responses table) , vendorid (which not in responses table , must determined based on couponid (which in both tables).
thanks assistance. it's appreciated.
could this:
select * coupons vendorid not in ( select c.vendorid responses r join coupons c on r.couponid = c.couponid r.respondentid = 3 , r.rejected = true group c.vendorid having sum(r.rejected) > 2 )
i've never used having
without including value in select
think work. also, not sure data type of rejected, sum() won't work.
Comments
Post a Comment