Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6612133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:03:34+00:00 2026-05-25T20:03:34+00:00

Hi I have a coupon system that uses one table (called coupons) to store

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:03:35+00:00Added an answer on May 25, 2026 at 8:03 pm

    For MySQL, joining is usually faster and more reliable than subqueries, but makes you think a bit harder. In this case, you need to limit based on the number of joined rows, which requires a post-grouping calculation; fortunately, that’s precisely what the HAVING clause is for:

    select coupons.*
    from coupons
    left join generatedcoupons on user_id = 233 and coupon_id = coupons.id
    where coupons.owner = 34
    group by coupons.id
    having count(generatedcoupons.id) < limit_per_user
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a coupon system that uses one table (called coupons) to store
I have a link that calls: <a href=/coupons/print_coupon/<?php echo $coupon['id']; } ?> id=print><span style=margin-left:24px;>Print</span></a>
I have a system that allows users to enter coupons (for example like dell
I have a PHP based website that uses coupon codes. I can restrict search
I have two simple entities: My\Entity\Coupon: type: entity table: coupon id: id: type: integer
I have to add a coupon table to my db. There are 3 types
I have Image which belongsTo User, and I have Coupon which belongsTo User. Each
I have the following 3 classes class User { static hasMany = [coupons: Coupon]
I have the code var expired_overlay=$('.expired-overlay'); expired_overlay.each(function(){ var t=$(this), p=$(this).closest('.coupon'), ph=p.height()+ ((p.css('padding-top').replace(px, ))*2); t.css({'width':p.width(),'height':ph,'opacity':0.5})
I have a Coupon controller. The index() function grabs a handful of coupons in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.