title: How to use a different table on an if (like) conditon in MySQL
body: <p>I have three tables
  First table(day) just holds values, ie</p>
  
  <pre><code>uid,title
  1, Mon
  2, Tue
  3, Wed
  etc,etc
  </code></pre>
  
  <p>The second table (lookup) uses the first table</p>
  
  <pre><code>uid,day_id,date,status,tech_id
  1, 1,2012-01-01, 1,700
  2, 2, 2012-01-02, 0,700
  etc,etc
  </code></pre>
  
  <p>The third table (template) uses the first table</p>
  
  <pre><code>uid,day_id,status,tech_id
  1, 1, 1,700
  2, 2, 0,700
  3, 3, 0,700
  </code></pre>
  
  <p>In reality, each table holds a little more info that is shown, but that data is irrelevant for now.</p>
  
  <p>What im attempting to do, is perform all the steps in a single SQL function rather than relying on code. Im trying to make the SQL function use the template table instead of the lookup table if there is no entry for that day</p>
  
  <p>In psudocode:</p>
  
  <pre><code>column 1 = * from days
  column 2 = status from(if (template != null, use template)elseif(lookup != null, use lookup)else(null))
  where tech_id=700
  </code></pre>
  
  <p>Is this at all posible in MySQL? If so, how? I've been trying to do it with INNER/LEFT(outer) joins and sub-queries, but so far to no avail :(</p>