title: How do I get MySQL to throw a conditional runtime exception in SQL
body: <p>I am writing a migration test to ensure that a user was created by the migration. If the user does not exist, the test should throw an error. At first, I thought I could just use a division by zero error to get what I wanted:</p>
  
  <pre><code>SET SESSION sql_mode = 'error_for_division_by_zero';
  SELECT 1/COUNT(*) FROM mysql.user WHERE user = 'foo';
  </code></pre>
  
  <p>However, this does not throw an error if <code>foo</code> does not exist. Turns out that <a href="https://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_error_for_division_by_zero" rel="nofollow noreferrer"><code>error_for_division_by_zero</code> affects only <code>INSERT</code> and <code>UPDATE</code> statements</a>.</p>
  
  <p>Then I thought maybe I could just call some function with the wrong number of arguments:</p>
  
  <pre><code>SELECT IF(COUNT(*) = 1, 1, date_format(1, 2, 3))
    FROM mysql.user WHERE user = 'foo';
  </code></pre>
  
  <p>But this dies even when <code>foo</code> does exist, presumably because the parser notices the incorrect parameter count.</p>
  
  <p>I could write a <a href="https://stackoverflow.com/a/16644480/79202">a function that emulates raising an exception</a>, but I was trying to avoid that. Is there no way to  coerce MySQL into conditionally throwing a runtime exception?</p>