title: Time calculations and counts with mysql
body: <p>This is my database layout:</p>
  
  <ul>
  <li>ID (int - unique primary key</li>
  <li>user (int) - foreign key for users I search by their <code>name</code> colum</li>
  <li>starttime (timestamp) - session start time</li>
  <li>endtime (timestamp) - session end time</li>
  <li>idletime (int) - idle time in minutes</li>
  </ul>
  
  <p>For example I want to know how many minutes a specific player was online.</p>
  
  <p>The following query works fine. I get about 3443 minutes.</p>
  
  <pre><code>SELECT user, 
         SUM(TIMESTAMPDIFF(MINUTE,starttime,endtime)-idletime) 
  FROM gc_sessions 
  WHERE user = 139
  </code></pre>
  
  <p>But if I want to do this with joins I get 8 by using the following query.</p>
  
  <pre><code> SELECT name, 
          SUM(TIMESTAMPDIFF(MINUTE,starttime,endtime)-idletime) 
   FROM gc_sessions 
   JOIN gc_users ON gc_user.id = gc_sessions.id
  WHERE name LIKE "variell"
  </code></pre>
  
  <p>What is wrong with my query?</p>