title: MySQL Select row-count if table exists
body: <p>I'm trying to get the row-count from a table, but only if it exists. (In one SQL-statement.)</p>
  
  <p>From <a href="https://stackoverflow.com/questions/18738909/mysql-select-if-table-exists">this</a> question, the answer does not return the row-count from the table if it exists. How can this be done without MySQL throwing the error 'Table does not exist'??</p>
  
  <p>My SQL is as follows:</p>
  
  <pre><code>SELECT CASE
      WHEN
          ( (SELECT COUNT(*) FROM `information_schema`.`tables` WHERE `table_schema` = DATABASE() AND `table_name` = 'testTbl') &gt; 0 )
      THEN
          (SELECT COUNT(*) FROM `testTbl`)
      ELSE
          (SELECT 0)
      END;
  </code></pre>
  
  <p>EDIT: The <code>WHEN</code>-part returns the right value (0 if the table doesn't exist, and 1 if it does).
  The real problem is that the entire SQL is not returning/selecting '0', when the tabel doesn't exist, but instead returns an error. (Because the non-existing table is mentioned in the <code>THEN</code>-clause, thereby defeating the whole purpose of previously checking if it exist. In other words, it can't ignore the <code>COUNT</code>-statement in the <code>THEN</code>-clause when the table doesn't exist.)</p>
  
  <p>This is a problem, because I want the returned value to be an integer (or numeric value).</p>