title: MySQL Query for ticketing system in custom back-end
body: <p>I'm trying to add a ticketing system to a custom back-end that powers the rest of the site. I'm having a bit of trouble though in ordering the tickets (once a reply has been made). I am fairly new to MySQL and not quite sure how to proceed.</p>
  
  <p>At the moment I have the following:</p>
  
  <pre><code>SELECT * FROM tbl_tickets ORDER BY ti_date_last_reply DESC, ti_date_raised DESC LIMIT 0, 20
  </code></pre>
  
  <p>The above works fine except that if a new ticket is raised the existing replies appear above it.</p>
  
  <p>I have 4 ticket status stored in <code>ti_status</code> - <strong>Open</strong> (a new ticket), <strong>Answered</strong> (admin has replied to ticket), <strong>Customer-Reply</strong> (customer has replied or added more info to ticket), and <strong>Closed</strong> (ticket is closed).</p>
  
  <p>What I would like is for the MySQL to order them by <code>ti_date_raised</code> if the ticket status is <strong>Open</strong> (oldest first), then add <strong>Customer-Reply</strong> which is found in <code>ti_date_last_reply</code> to slot into the correct time frame. Leaving <strong>Answered</strong> and <strong>Closed</strong> to appear below these queries (oldest to youngest) - but <strong>Answered</strong> goes above <strong>Closed</strong></p>
  
  <p>Example (I used date/time here, but I use Unix time in the scripts):</p>
  
  <pre><code>Date Raised      |   Date Replied   | Status  
  ----------------------------------------------------------  
  2011-01-30-20-30 | -                | Open  
  2011-01-31-01-00 | 2011-02-01-09-30 | Customer-Reply  
  2011-02-01-10-30 | -                | Open  
  2011-02-01-20-00 | 2011-02-01-20-30 | Answered  
  2011-02-01-21-00 | 2011-02-01-20-35 | Closed
  </code></pre>
  
  <p>Is such a thing possible?</p>