title: Selecting from mySQL based on timeinterval
body: <p>So... My brain has malfunctioned - the logic is gone and I'm frustrated!</p>
  
  <p>I'm building a calender. I have a table with appointments:</p>
  
  <pre><code>------------------------------------------------------------------------
  | id | user_id | app_name  | app_start           | app_end             |
  ------------------------------------------------------------------------
  | 1  | 3       | Meeting   | 2014-05-29 15:00:00 | 2014-05-29 17:00:00 |
  ------------------------------------------------------------------------
  </code></pre>
  
  <p>Then the user can input a date and a time, and then a time interval.</p>
  
  <p>Let's say the user selects 2014-05-29, 15:15 and sets the time interval to +2 hours.
  This gives us two datetimes for comparison:</p>
  
  <pre><code>$start  =   '2014-05-29 15:15:00';
  $end    =   '2014-05-29 17:15:00';
  </code></pre>
  
  <p>Now, I want to select all appointments in the calender which "conflict" with those two datetimes. However, appointments that end on $start should not be selected and appointments that starts on $end should not be selected.</p>
  
  <p>Should be easy, but I haven't nailed it yet. The query I ended up with is this:</p>
  
  <pre><code>SELECT id, app_name
  FROM appointments
  WHERE user_id = ?
    AND ((app_start BETWEEN '$start' AND '$end' AND app_start != '$end')
    OR (app_end BETWEEN '$start' AND '$end')
    OR (app_start &gt; '$start' AND app_end &gt; '$end'))
  </code></pre>
  
  <p>So what I'm asking is: How should my query look?</p>
  
  <p>EDIT:</p>
  
  <p>Based on answers to my question, the working query was this:</p>
  
  <pre><code>SELECT id, app_name FROM appointments 
  WHERE user_id = ? 
    AND ((app_start BETWEEN '$start' AND '$end' AND app_start != '$end') 
    OR (app_end BETWEEN '$start' AND '$end' AND app_end != '$start') 
    OR (app_start &lt; '$start' AND app_end &gt; '$end'))
  </code></pre>