title: Check if Coupon exists and if it is the first order
body: <p>In my e-commerce I have implemented coupons, and it is working, but now I introduce "Coupons first order", but don't know if I'm doing the best way to check it, because it is not working, my query is ignoring this consult.</p>
  
  <p>How I can check if <code>coupon_tp</code> i'ts first, and If is true, how check if user is first order?</p>
  
  <pre><code>public function verificaCupom($coupon){
      $this-&gt;db-&gt;select("customer_id, id, value, percent, code, discount_tp, coupon_tp");
  
  //check if is first order (not working)
      $this-&gt;db-&gt;select("IF(ga845_cupons.coupon_tp = 'first', (SELECT customer_id FROM ga845_pedidos_view WHERE NOT EXISTS 'ga845_pedidos_view.customer_id = ga845_cupons.customer_id'))  as primeira");
  
  //check if coupon exist
      $this-&gt;db-&gt;where('code', $coupon);
  //check valid
      $this-&gt;db-&gt;where('CURDATE() &lt;= valid');
  //check if coupon it's enable
      $this-&gt;db-&gt;where('status', 0);
      $this-&gt;db-&gt;limit("1");
  
      $query = $this-&gt;db-&gt;get('ga845_cupons');
  
      return $query-&gt;result();
  }
  </code></pre>