title: Returning a value even if no result
body: <p>I have this kind of simple query that returns a not null integer field for a given id:</p>
  
  <pre><code>SELECT field1 FROM table WHERE id = 123 LIMIT 1;
  </code></pre>
  
  <p>The thing is if the id is not found, the resultset is empty. I need the query to always return a value, even if there is no result.</p>
  
  <p>I have this thing working but I don't like it because it runs 2 times the same subquery:</p>
  
  <pre><code>SELECT IF(EXISTS(SELECT 1 FROM table WHERE id = 123) = 1, (SELECT field1 FROM table WHERE id = 123 LIMIT 1), 0);
  </code></pre>
  
  <p>It returns either field1 if the row exists, otherwise 0. Any way to improve that?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit following some comments and answers</strong>: yes it has to be in a <strong>single query statement</strong> and I can not use the count trick because <strong>I need to return only 1 value</strong> (FYI I run the query with the Java/Spring method SimpleJdbcTemplate.queryForLong()).</p>