title: SQL Query - Exists in other table or does not
body: <p>Apologies for the somewhat confusing title. It's difficult to summarise the issue in a couple of words. I am making a request to a database, on the following tables:</p>
  
  <p><code>Event</code>:</p>
  
  <pre><code>ID, startdate, offtime, meetingid reason
  </code></pre>
  
  <p><code>Meeting</code>:</p>
  
  <pre><code>ID, description
  </code></pre>
  
  <p><code>cm_delay_reasons</code>:</p>
  
  <pre><code>ID, reasoncode, reasondescription
  </code></pre>
  
  <p>The tuples returned need to match the following criteria:</p>
  
  <ul>
  <li>The <code>MeetingID</code> in <code>Event</code> needs to map to an <code>ID</code> in <code>Meeting</code>.</li>
  <li>The <code>reason</code> value in <code>Event</code> can not be null</li>
  <li>The <code>reason</code> value in <code>Event</code> <em>can</em> map to a <code>reasoncode</code> in <code>cm_delay_reasons</code>.
  <ul>
  <li>The <code>reason</code> value can also not map, but must not be null.</li>
  <li>If it isn't null, then don't display a reason description.</li>
  </ul></li>
  </ul>
  
  <p>The reason is for the latter part is simple. <code>Event.Reason</code> can either be a code, indicating it matches one of our preconfigured delays for an event. It can also be plaintext, indicating that the user decided their own reason for a delay. The aim is to get both of these, and for those values that do not map, leave the reason description empty.</p>
  
  <p>The SQL query I have come up with is:</p>
  
  <pre><code>select 
      m.description, e.startdate, e.offtime, e.reason, d.reasondescription 
  from 
      event e, meeting m, cm_delay_reasons d 
  where 
      e.meetingid = m.id 
      and (d.reasoncode = e.reason);
  </code></pre>
  
  <p>However, ths isn't displaying the custom reasons that a user can put in. Only the predefined ones that exist in the <code>cm_delay_reasons</code> table, so I changed my tact and attempted:</p>
  
  <pre><code>select 
      m.description, e.startdate, e.offtime, e.reason, d.reasondescription 
  from 
      event e, meeting m, cm_delay_reasons d 
  where 
      e.meetingid = m.id 
      and (e.reason is not null);
  </code></pre>
  
  <p>However this is causing duplicates. What I need to work out is how to marry the functionality of the two.</p>