title: MySQL insert only if a condition is true
body: <p>I have several insert statements that I only want to execute if they are true.</p>
  
  <p>Here is my example:</p>
  
  <pre><code>START TRANSACTION;
  
  INSERT INTO fields (field_name, control_type_id, needs_approval)
  VALUES 
  ('Array Photos', 3, 0);
  
  INSERT INTO field_to_job_type (field_id, job_type_id, sequence_number, parent_id)
  VALUES
  (last_insert_id(), (SELECT job_type_id FROM job_types WHERE job_type_name = 'Cash'), 1, (SELECT field_id FROM fields where field_name = 'Photo Pack'));
  
  COMMIT;
  </code></pre>
  
  <p>Now, I only really want to run the second query if there is only 1 result for this query: </p>
  
  <p><code>SELECT job_type_id FROM job_types WHERE job_type_name = 'Cash'</code>.</p>
  
  <p>If that query returns 2 results, I would want to abandon the transaction.  </p>
  
  <p>Is that possible?  Something I tried:</p>
  
  <pre><code>START TRANSACTION;
  INSERT ...;
  INSERT ...;
  IF(SELECT count(job_type_name) FROM job_types WHERE job_type_name = 'Cash') = 1, 'COMMIT', 'ROLLBACK');
  </code></pre>
  
  <p>Which, obviously didn't work.</p>