title: Dynamically generate criteria in SQL
body: <p>I have a Users table that contains dozens of columns like date of birth, year of vehicle owned, make and model of the vehicle, color and many other personal fields unrelated to the vehicle</p>
  
  <p>There's also a 2nd table called Coupons that needs to be designed in a way to support a qualification like "user qualifies if younger than 30 yrs old", "user qualifies if vehicle is greater than 10 yrs old", "user qualifies if vehicle color is green".</p>
  
  <p>When a user logs in, I need to present all coupons the user qualifies for. The problem that I'm having is that the coupon qualifications could be numerous, could have qualifiers like equal, greater than or less than and may have different combinations.</p>
  
  <p>My only solution at this point is to store the actual sql string within one of the coupons table columns like </p>
  
  <pre><code>select * from Users where UserId = SOME_PLACEHOLDER and VehicleYear &lt; 10
  </code></pre>
  
  <p>Then I could execute the sql for each coupon row and return true or false. Seems very inefficient as I would potentially have to execute 1000s of sql statements for each coupon code.</p>
  
  <p>Any insight, help is appreciated. I do have server-side code where I could potentially be able to do looping.</p>
  
  <p>Thank you.</p>