title: MySQL Query For Total Online Time Based On Login/Logout Entries
body: <p>Ok, so I have to perform a bit of a nasty MySQL query, and I can't for the life of me think of how to do it.</p>
  
  <p>I have the following content:</p>
  
  <pre><code>id    date                       name    join_or_leave
  1     yyyy-mm-dd hh-mm-ss        user1   join
  2     yyyy-mm-dd hh-mm-ss        user2   join
  3     yyyy-mm-dd hh-mm-ss        user1   leave
  4     yyyy-mm-dd hh-mm-ss        user3   join
  5     yyyy-mm-dd hh-mm-ss        user2   leave
  6     yyyy-mm-dd hh-mm-ss        user1   join
  7     yyyy-mm-dd hh-mm-ss        user4   join
  8     yyyy-mm-dd hh-mm-ss        user1   leave
  </code></pre>
  
  <p>So it logs all login/logout times for users.</p>
  
  <p>What I have to calculate is the total time spent online (the total for all users, but I can work out this total after getting the total for each user if getting the total of all users is going to be very slow).</p>
  
  <p>Hopefully it's obvious what I need to do here, obviously all of the dates  are going downwards (so whatever time the  join/leave entry was added); but how I'd go about doing it I just don't know. :p</p>