Pulling some coupons from a database. Each coupon has a merchantid column that contains the id for the merchant for which the coupon belongs too.
I’m trying to construct a query that pulls 5 coupons, but I only want 1 coupon per merchantid. I don’t want multiple coupons with the same merchantid.
I have some WHERE conditions that need to go in there as well. This what I have so far,its pulling only 1 coupon per merchant, but the coupon its pulling doesn’t have the highest clicks:
SELECT C.couponid,C.fmtc_couponid,C.merchantid,
C.label,C.restrictions,C.coupon,C.link,
C.image,C.expire,C.unknown,C.clicks,M.name,
M.approved,M.homepageurl,M.logo_image,M.permalink
FROM tblCoupons C,tblMerchants M
WHERE C.merchantid=M.merchantid AND
C.begin < ".mktime()." AND
C.expire > ".mktime()." AND
M.display='1'
GROUP BY C.merchantid ORDER BY C.clicks DESC LIMIT 0,5
I think what you need is to pre-query from the coupons table, group by the merchant, then re-join back.