title: Can I refactor my MySQL queries into one query based on number of results?
body: <p>I have a table <code>y</code> Which has two columns <code>a</code> and <code>b</code></p>
  
  <p>Entries are:</p>
  
  <pre><code>a   b
  
  1   2
  
  1   3
  
  1   4
  
  0   5
  
  0   2
  
  0   4
  </code></pre>
  
  <p>I want to get 2,3,4 if I search column <code>a</code> for 1, and 5,2,4 if I search column <code>a</code>.</p>
  
  <p>So, if I search A for something that is in A, (1) I get those rows, and if there are no entries A for given value, give me the 'Defaults' (<code>a</code> = '0')</p>
  
  <p>Here is how I would know how to do it:</p>
  
  <pre><code>$r = mysql_query('SELECT `b` FROM `y` WHERE `a` = \'1\';');
  //This gives desired results, 3 rows
  
  $r = mysql_query('SELECT `b` FROM `y` WHERE `a` = \'2\';');
  //This does not give desired results yet.
  //Get the number of rows, and then get the 'defaults'
  if(mysql_num_rows($r) === 0) $r = mysql_query('SELECT `b` FROM `y` WHERE `a` = 0;');
  </code></pre>
  
  <p>So, now that it's sufficiently explained, how do I do that in one query, and what about performance concerns?  </p>
  
  <p>The most used portion would be the third query, because there would only be values in <code>a</code> for a number IF you stray from the defaults.</p>