title: INSERT INTO if conditions are met
body: <p>A user is only meant to have up to 3 keys registered to his account at any one time. To add a new key, the user must first delete another key to "make room" for a new one.</p>
  
  <p>I want this to be checked server-side, but I can't get the query to work. Here is what I tried:</p>
  
  <pre><code>IF (SELECT COUNT(serial_key_nbr)
  FROM keys_table WHERE user_id = 9) &lt;= 2
  THEN INSERT INTO keys_table (user_id, serial_key_nbr)
  VALUES (9, 'abc123')
  </code></pre>
  
  <p>How to do this?</p>