title: MySQL: Select only the earliest date found in a set
body: <p>I have to write an export function that creates a CSV file of shop-orders, that have specific statuses attached to them within a specific timeframe. The problem is, that one kind of status (like status ID 32) can appear multiple times within the same order, but only the earliest added status is relevant.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>select * FROM orders_status_history
  JOIN orders_total ON (
      orders_total.orders_id = orders_status_history.orders_id 
      AND orders_total.class = "ot_total"
  )
  JOIN orders ON (
      orders.orders_id = orders_status_history.orders_id
  )
  WHERE orders_status_history.orders_status_id IN(32, 21, 56, 45)
    AND orders_status_history.date_added &gt;= "' . $start . '" 
    AND orders_status_history.date_added &lt;= "' . $end . '" 
  </code></pre>
  
  <p>This already works as expected, <strong>the goal is to only return a record when the matching status was also the earliest added to the order.</strong></p>