Jump to content

help with GROUP BY


schilly

Recommended Posts

Here is one query I have that works:

SELECT company,

  retail_vendors.id as retail_id,

  COUNT(retail_codes.id) as coupons_assigned,

  SUM(IF(redeemed=1,1,0)) as coupons_used,

  SUM(IF(shipped=1,1,0)) as shipped,

  SUM(IF(redeemed=0,1,0)) as coupons_left

FROM retail_vendors LEFT JOIN retail_codes ON retail_vendors.id = vendor

WHERE status = 1

GROUP BY retail_id ORDER BY company

 

Now I want to select vendors with <= 5 coupons left. I'm trying this:

SELECT company, retail_vendors.id as retail_id,

SUM(IF(redeemed=0,1,0)) as coupons_left

FROM retail_vendors LEFT JOIN retail_codes ON retail_vendors.id = vendor

WHERE SUM(IF(redeemed=0,1,0)) <= 5 GROUP BY retail_vendors.id ORDER BY company

 

but I keep getting an error: "invalid use of group function". What's the issue here or is there a better way to do this? I tried WHERE coupons_left <= 5 and that didn't work either.

Link to comment
https://forums.phpfreaks.com/topic/224375-help-with-group-by/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.