Hi I have a coupon system that uses one table (called "coupons") to store info about each type of available coupon that one could generate, and another table (generatedcoupons) to store info about each coupon generated. I'm having a hard time comparing info in each table.
The schemas:
table: coupons
+----+------+--------------------+---------------+
| id| owner| expiration_date| limit_per_user|
| 15| 34| 2011-09-18 00:00:00| 2|
+----+------+--------------------+---------------+
table: generatedcoupons
+----+----------+------+--------------------+------+--------+
| id| coupon_id| owner| date| used| user_id|
| 1| 15| 34| 2011-09-17 00:00:00| false| 233|
+----+----------+------+--------------------+------+--------+
I'm trying to run queries to display coupon from the point of view of a user (i.e. all queries will have where user_id='$userid'. I can't figure out how to display all coupons where the limit_per_user has not been met... here's what I've got that doesn't work:
select *
from coupons
where owner=34
and (count(SELECT * from generatedcoupons where user_id=233 and coupon_id=coupons.id)<limit_per_user)