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| date_expiration| 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 select all expired coupons for a given user. Is this correct?
select *
from coupons
join generatedcoupons on user_id = 233 and coupon_id=coupons.id
where coupons.owner=34 and (curdate()>date(coupons.date_expiration)
You need to join on the owner and the coupon id. Then put the values that you want to filter by in the where clause.