こんにちは私は、1 つのテーブル (「クーポン」と呼ばれる) を使用して、生成できる利用可能なクーポンの各タイプに関する情報を格納し、別のテーブル (生成されたクーポン) を使用して、生成された各クーポンに関する情報を格納するクーポン システムを持っています。各テーブルの情報を比較するのに苦労しています。
スキーマ:
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|
+----+----------+------+--------------------+------+--------+
ユーザーの観点からクーポンを表示するクエリを実行しようとしています (つまり、すべてのクエリには がありwhere user_id='$userid'ます。 limit_per_user が満たされていないすべてのクーポンを表示する方法がわかりません... ここにそれはうまくいきません:
select * 
from coupons 
where owner=34 
and (count(SELECT * from generatedcoupons where user_id=233 and coupon_id=coupons.id)<limit_per_user)